开发者

Is there a way to know how much data is available in a Python socket to receive?

开发者 https://www.devze.com 2023-03-27 21:57 出处:网络
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

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:

    开发者_如何学运维
  1. What is an equivalent to FIONREAD in Python? How do I call sock.ioctl() to obtain the amount of bytes available?
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号