Is there a way to know how much data is available in a Python socket to receive?
I have figured out that I must use ioctl. There are similar questions here:
- How to tell how much data is in a Socket's send buffer
- Determing the number of bytes ready to be recv()'d
My questions are:
-
开发者_如何学运维
- What is an equivalent to FIONREAD in Python? How do I call sock.ioctl() to obtain the amount of bytes available?
- What if I am using Python 2.5 on Windows? socket.ioctl is new in version 2.6.
As there is no portable way of doing this (and often the answer is wrong), there isn't a built-in way of asking for it.
If you use non-blocking sockets, simply read as much data as possible, and only data which is present will be returned.
You could get that number, but the number is less than useful. There is a window of time between the kernel reads the number and your code gets to actually use it. In that window more data can arrive on the socket. In other words, this number is likely to be stale.
精彩评论