Dominik Macháček commited on
Commit
706b7f8
·
1 Parent(s): c812334

sending line packets without zero padding

Browse files
Files changed (1) hide show
  1. line_packet.py +3 -3
line_packet.py CHANGED
@@ -16,7 +16,7 @@ containing:
16
  PACKET_SIZE = 65536
17
 
18
 
19
- def send_one_line(socket, text):
20
  """Sends a line of text over the given socket.
21
 
22
  The 'text' argument should contain a single line of text (line break
@@ -36,12 +36,12 @@ def send_one_line(socket, text):
36
  lines = text.splitlines()
37
  first_line = '' if len(lines) == 0 else lines[0]
38
  # TODO Is there a better way of handling bad input than 'replace'?
39
- data = first_line.encode('utf-8', errors='replace') + b'\n\0'
40
  for offset in range(0, len(data), PACKET_SIZE):
41
  bytes_remaining = len(data) - offset
42
  if bytes_remaining < PACKET_SIZE:
43
  padding_length = PACKET_SIZE - bytes_remaining
44
- packet = data[offset:] + b'\0' * padding_length
45
  else:
46
  packet = data[offset:offset+PACKET_SIZE]
47
  socket.sendall(packet)
 
16
  PACKET_SIZE = 65536
17
 
18
 
19
+ def send_one_line(socket, text, pad_zeros=False):
20
  """Sends a line of text over the given socket.
21
 
22
  The 'text' argument should contain a single line of text (line break
 
36
  lines = text.splitlines()
37
  first_line = '' if len(lines) == 0 else lines[0]
38
  # TODO Is there a better way of handling bad input than 'replace'?
39
+ data = first_line.encode('utf-8', errors='replace') + b'\n' + (b'\0' if pad_zeros else b'')
40
  for offset in range(0, len(data), PACKET_SIZE):
41
  bytes_remaining = len(data) - offset
42
  if bytes_remaining < PACKET_SIZE:
43
  padding_length = PACKET_SIZE - bytes_remaining
44
+ packet = data[offset:] + (b'\0' * padding_length if pad_zeros else b'')
45
  else:
46
  packet = data[offset:offset+PACKET_SIZE]
47
  socket.sendall(packet)