From 706b7f847e42bc68bc23ff64241b7a51ed690eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Mach=C3=A1=C4=8Dek?= Date: Wed, 21 Feb 2024 15:36:16 +0100 Subject: [PATCH] sending line packets without zero padding --- line_packet.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/line_packet.py b/line_packet.py index 364ade2..54dea11 100644 --- a/line_packet.py +++ b/line_packet.py @@ -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)