Python recv loop

MRAB python at
Mon Feb 11 16:11:46 CET 2013

On 2013-02-11 14:56, Ihsan Junaidi Ibrahim wrote:
> Hi Roy,
> On Feb 11, 2013, at 10:24 AM, Roy Smith <roy at> wrote:
>> Is this server that you're talking to something that you have control
>> over, i.e. are you stuck with this protocol?  Given a choice, I'd go
>> with something like JSON, for which pre-existing libraries for every
>> language under the sun.
> I'm running JSON for my application messaging protocol but with JSON and python default unordered dict,
> there's no guarantee if I put in the length key in the JSON message, it will be placed on the first bytes hence
> why it was designed for a fixed 4-byte at the start of the message to indicate the message length.
> Beyond the 4-bytes it is all JSON.
> but if you have a better idea, i would certainly welcome it.
I probably wouldn't make it fixed length. I'd have the length in
decimal followed by, say, "\n".

>> Do you actually *know* what the value of nbuf is?  Is it possible that
>> (somehow) it's 0?  You should print (log, whatever), the value of nbuf,
>> just to make sure.
> nbuf is printing the right bytes amount, I removed the print statement before I made the first post.
> So to clarify, I added a print statement between the first recv and the second.
> {"msgver": "1.0", "msgid": "200", "subcode": "100", "appver": "1.0", "appid": "1.0", "data": {"1": "igb0", "2": "igb1", "ifcnt": "2"}}
> connected to misty:8080
> sending data
> 138 bytes sent: 0x86{"msgver": "1.0", "msgid": "200", "subcode": "100", "appver": "1.0", "appid": "1.0", "data": {"1": "igb0", "2": "igb1", "ifcnt": "2"}}
> receiving data
> message length is 188
> 0 bytes received:
> So the subsequent recv() call will be readjusted with 188 bytes buffer size so theoretically, recv shouldn't return 0.
> The same logic that I used to send to the server from the python client that the server will readjust the second recv() call based on the length information. On this 2nd recv() call the server is able to obtain the rest of the messages.

More information about the Python-list mailing list