Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38718

Re: Python recv loop

Date 2013-02-12 03:09 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: Python recv loop
References (2 earlier) <E4DA0A42-324E-43D0-A366-21267C538D5D@grep.my> <51190A32.7070105@mrabarnett.plus.com> <CAPTjJmoe0CJKEFc0dDZr9F68kNyerM1Fp8_ACpWRWjvU6sdtZg@mail.gmail.com> <6F9402BA-4753-4543-A8E7-05E5234660EA@grep.my> <CAPTjJmpGYJqofaL_R6W-TUTaRGTh=f4XVDASmskQKnRnCnu+CA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1678.1360638583.2939.python-list@python.org> (permalink)

Show all headers | View raw


On 2013-02-12 02:20, Chris Angelico wrote:
> On Tue, Feb 12, 2013 at 12:41 PM, Ihsan Junaidi Ibrahim <ihsan@grep.my> wrote:
>>
>> On Feb 11, 2013, at 11:24 PM, Chris Angelico <rosuav@gmail.com> wrote:
>>
>>> On Tue, Feb 12, 2013 at 2:11 AM, MRAB <python@mrabarnett.plus.com> wrote:
>>>> I probably wouldn't make it fixed length. I'd have the length in
>>>> decimal followed by, say, "\n".
>>>
>>> Or even "followed by any non-digit". Chances are your JSON data begins
>>> with a non-digit, so you'd just have to insert a space in the event
>>> that you're JSON-encoding a flat integer. (Which might not ever
>>> happen, if you know that your data will always be an object.)
>>>
>>> ChrisA
>>
>> So on the first recv() call, I set the buffer at 1 character and I iterate
 >> over single character until a non-digit character is encountered?
>
> More efficient would be to guess that it'll be, say, 10 bytes, and
> then retain any excess for your JSON read loop. But you'd need to sort
> that out between the halves of your code.
>
If the length is always followed by a space then it's easier to split
it off the input:

     buf = sock.recv(10)
     space_pos = buf.find(b" ")
     nbuf = int(buf[ : space_pos])
     buf = buf[space_pos+ 1 : ]

     while len(buf) < nbuf:
         chunk = sock.recv(nbuf - len(buf))
         if not chunk:
             break

         buf += chunk

I'm assuming that:

1. The initial recv returns the length followed by a space. It could,
of course, return fewer bytes (space_pos == -1), so you may need to
recv some more bytes, like what's done later on.

2. At least 10 bytes were sent. Imagine what would happen if the sender
sent b"2 []" immediately followed by b"2 []". The initial recv could
return all of it. In that case you could save the excess until next
time. Alternatively, the sender could guarantee that it would never
send fewer than the 10 bytes, padding with several b" " if necessary.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Python recv loop Ihsan Junaidi Ibrahim <ihsan@grep.my> - 2013-02-11 08:48 +0800
  Re: Python recv loop Roy Smith <roy@panix.com> - 2013-02-10 21:24 -0500
    Re: Python recv loop Ihsan Junaidi Ibrahim <ihsan@grep.my> - 2013-02-11 22:56 +0800
      Re: Python recv loop Roy Smith <roy@panix.com> - 2013-02-11 21:44 -0500
    Re: Python recv loop MRAB <python@mrabarnett.plus.com> - 2013-02-11 15:11 +0000
    Re: Python recv loop Chris Angelico <rosuav@gmail.com> - 2013-02-12 02:24 +1100
    Re: Python recv loop Ihsan Junaidi Ibrahim <ihsan@grep.my> - 2013-02-12 09:41 +0800
    Re: Python recv loop Chris Angelico <rosuav@gmail.com> - 2013-02-12 13:20 +1100
    Re: Python recv loop MRAB <python@mrabarnett.plus.com> - 2013-02-12 03:09 +0000

csiph-web