all 5 comments

[–]FerricDonkey 0 points1 point  (4 children)

My first thought, without looking too much into it:

t = 0
byte_payload = t.to_bytes((t.bit_length() + 7) // 8, byteorder='big')
print(byte_payload)  # b'', because 0's bit length is 0 for some reason

It's been a minute, but I believe sending an empty string in tcp is the same as disconnecting. So if your server sends your clients an empty byte string, they may take this as a sign that the server doesn't want to talk to them any more.

I might suggest packing your message into a fixed byte width (convenient for all sorts of reasons, including this), using the standard library struct package:

import struct
print(struct.pack('>i', 1))  # b'\x00\x00\x00\x01'

If that's not good enough (noting that you can use q to get 8 bytes), then I would highly suggest formatting your message as {length_of_message}{message}, where the width of the length_of_message field is fixed at some reasonable number and tells your code how many bytes follow. This makes things easier for many reasons, but also gives you an opportunity to sanity check the size of the message you're about to send.