strange sockets

Bryan Olson fakeaddress at nowhere.org
Fri Nov 4 15:25:42 EST 2005


Skink wrote:
[...]

> what's wrong here?

Sion Arrowsmith is right about what causes the delay.
Just in case your real code looks like this, I'll note:

>     len, = struct.unpack("!i", s.recv(4))
>     data = s.recv(len)

First, you almost certainly don't want to use the name 'len'.
Ought not to be allowed. Second, recv can return fewer bytes
than requested, even when the connection is still open for
reading. You might replace the lines above with (untested):

     length = struct.unpack("!i", s.recv(4))
     data = []
     while length:
         data.append(s.recv(length))
         length -= len(data[-1])
     data = ''.join(data)


There's still a robustness problem, but in the absense of errors
and malice, that should work. I think.


-- 
--Bryan



More information about the Python-list mailing list