Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Python recv loop (http://www.velocityreviews.com/forums/t957516-re-python-recv-loop.html)

Dave Angel 02-11-2013 03:17 PM

Re: Python recv loop
 
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


All times are GMT. The time now is 10:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57