determining how much data is on a socket

Matthew Dixon Cowles matt at mondoinfo.com
Sat Dec 16 18:45:51 EST 2000


On Sat, 16 Dec 2000 18:33:51 -0500, Z 3 Penguin
<z3penguin at penguinpowered.com> wrote:

>is there any way to find out how much data is waiting on a given
>socket?

Not directly.

>my program reads 2 bytes from the socket to know how much more it
>needs to read.  however, sometimes it reads those two bytes and
>pauses until the rest of the data comes in.  i would like to know if
>the bytes are waiting before i read, so that if they are not, my
>program can continue and try to read it later.  any ideas?

What you can do is check the socket for waiting data with a short
timeout specified. Use either the standard select module, described
at:

http://www.python.org/doc/current/lib/module-select.html

or one of the modules that adds timeouts to the sockets themselves,
described at:

http://www.vex.net/parnassus/apyllo.py?i=87800233

and

http://www.vex.net/parnassus/apyllo.py?i=97250001

I haven't yet used either of those but they look like fun.

Regards,
Matt



More information about the Python-list mailing list