sending line packets without zero padding
This commit is contained in:
parent
c8123344c6
commit
706b7f847e
1 changed files with 3 additions and 3 deletions
|
|
@ -16,7 +16,7 @@ containing:
|
|||
PACKET_SIZE = 65536
|
||||
|
||||
|
||||
def send_one_line(socket, text):
|
||||
def send_one_line(socket, text, pad_zeros=False):
|
||||
"""Sends a line of text over the given socket.
|
||||
|
||||
The 'text' argument should contain a single line of text (line break
|
||||
|
|
@ -36,12 +36,12 @@ def send_one_line(socket, text):
|
|||
lines = text.splitlines()
|
||||
first_line = '' if len(lines) == 0 else lines[0]
|
||||
# TODO Is there a better way of handling bad input than 'replace'?
|
||||
data = first_line.encode('utf-8', errors='replace') + b'\n\0'
|
||||
data = first_line.encode('utf-8', errors='replace') + b'\n' + (b'\0' if pad_zeros else b'')
|
||||
for offset in range(0, len(data), PACKET_SIZE):
|
||||
bytes_remaining = len(data) - offset
|
||||
if bytes_remaining < PACKET_SIZE:
|
||||
padding_length = PACKET_SIZE - bytes_remaining
|
||||
packet = data[offset:] + b'\0' * padding_length
|
||||
packet = data[offset:] + (b'\0' * padding_length if pad_zeros else b'')
|
||||
else:
|
||||
packet = data[offset:offset+PACKET_SIZE]
|
||||
socket.sendall(packet)
|
||||
|
|
|
|||
Loading…
Reference in a new issue