Sockets: Sending/receiving arbitrary amounts of data

Doobee R. Tzeck drt at un.bewaff.net
Tue May 1 10:57:04 CEST 2001


Greg Ewing <see at my.signature> writes:

> Neil Schemenauer wrote:
> > 
> >         s = sock.recv(size)
> >         if len(s) != size:
> >             raise IOError, "short netstring read"
> 
> That's not quite right, either, because sock.recv(size)
> may return before all size bytes have arrived. You
> need to put it in a loop, something like
> 
>   s = ""
>   while len(s) < size:
>     s1 = sock.recv(size - len(s))
>     if s1 == "":
>       raise IOError, "unexpected EOF on socket"
>     s = s + s1

Comming from a C background I have written such code many
times. Considering Python is a high level Language I ask myself
if there shouldend be a way in Python to optionally shield the
user (programmer) from the hassle of handling of short socket
reads.

drt

-- 
Crypto Anarchy: encryption, digital money, anonymous networks,
digital pseudonyms, zero knowledge, reputations, information markets,
black markets, collapse of governments, freedom.

http://koeln.ccc.de/~drt/



More information about the Python-list mailing list