I want to verify if the socket data buffer is empty or not before calling socket.recv(bufsize[, flags]). Is there a way to do that?
You can peek (look without actually consuming the data):
data = conn.recv(bufsize, socket.MSG_PEEK)
You might want to make your socket non-blocking:
socket.setblocking(0)
After that call, if you read from a socket without available data it will not block, but instead an exception is raised. See socket.setblocking(flag) for details.
For more advanced uses, you might look at select. Something like:
r, _, _ = select([socket],[],[],0)
# ^
# Timeout of 0 to poll (and not block)
Will inform you if some data are available to read in your socket.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With