Dominik Macháček 2024-02-21
sending line packets without zero padding
@854ddf1ae743153ab826825b243bc3121d777526
line_packet.py
--- line_packet.py
+++ line_packet.py
@@ -16,7 +16,7 @@
 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 @@
     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)
Add a comment
List