Page 1 of 1

How to know how many bytes available to usocket.recv() ?

Posted: Tue Jul 30, 2019 10:41 pm
by water
How to know how many bytes available to usocket.recv() ?
:?:
Thanks.

Re: How to know how many bytes available to usocket.recv() ?

Posted: Wed Jul 31, 2019 5:53 am
by pythoncoder
There is no way to know in advance how many bytes will be received (as far as I know). When you issue the .recv() method you will get back as many bytes as were available in the timeout period. You can then check the size of the received bytes object or parse it and look for a delimiter.

Code: Select all

bytes_rx = my_socket.recv()
print(len(bytes_rx))
eol = bytes_rx.find(b'\n')
if eol > 0:
    # Got a line of text
else:
    # need to try again and concatenate