

sending line packets without zero padding
@854ddf1ae743153ab826825b243bc3121d777526
--- line_packet.py
+++ line_packet.py
... | ... | @@ -16,7 +16,7 @@ |
16 | 16 |
PACKET_SIZE = 65536 |
17 | 17 |
|
18 | 18 |
|
19 |
-def send_one_line(socket, text): |
|
19 |
+def send_one_line(socket, text, pad_zeros=False): |
|
20 | 20 |
"""Sends a line of text over the given socket. |
21 | 21 |
|
22 | 22 |
The 'text' argument should contain a single line of text (line break |
... | ... | @@ -36,12 +36,12 @@ |
36 | 36 |
lines = text.splitlines() |
37 | 37 |
first_line = '' if len(lines) == 0 else lines[0] |
38 | 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' |
|
39 |
+ data = first_line.encode('utf-8', errors='replace') + b'\n' + (b'\0' if pad_zeros else b'') |
|
40 | 40 |
for offset in range(0, len(data), PACKET_SIZE): |
41 | 41 |
bytes_remaining = len(data) - offset |
42 | 42 |
if bytes_remaining < PACKET_SIZE: |
43 | 43 |
padding_length = PACKET_SIZE - bytes_remaining |
44 |
- packet = data[offset:] + b'\0' * padding_length |
|
44 |
+ packet = data[offset:] + (b'\0' * padding_length if pad_zeros else b'') |
|
45 | 45 |
else: |
46 | 46 |
packet = data[offset:offset+PACKET_SIZE] |
47 | 47 |
socket.sendall(packet) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?