Python recv loop

Dave Angel davea at davea.name
Mon Feb 11 10:17:27 EST 2013


On 02/11/2013 10:02 AM, Ihsan Junaidi Ibrahim wrote:
>
>   <snip>
>
>      print 'message length is {0}'.format(nbuf)
>
>      while True:
>          buf = sock.recv(nbuf)
>
>          if not buf:
>              break

This loop doesn't terminate till buf is zero length, which it will be 
eventually.  At that point, you've overwritten the real data you may 
have gotten.  So the loop is just plain wrong.

Uwe MRAB's code, since there's no promise that all the data will be 
returned in a single call.  Keep accumulating it as you loop.


>
>      slen = len(buf)
>      str = "{0} bytes received: {1}".format(slen, buf)
>      print str
>      return 0




-- 
DaveA



More information about the Python-list mailing list