Python recv loop

Ihsan Junaidi Ibrahim ihsan at
Mon Feb 11 15:56:32 CET 2013

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.

> 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