Velocity Reviews

Velocity Reviews (
-   Python (
-   -   Re: Python recv loop (

Ihsan Junaidi Ibrahim 02-11-2013 03:02 PM

Re: Python recv loop
Hi Dave,

On Feb 11, 2013, at 9:22 AM, Dave Angel <> wrote:

> Exactly how are you sending "hexadecimal" ? If that 0xad (which is only one byte, what about the other 3 ?) is intended to be a C description, then it's certainly not hex, it's binary. And probably little-endian, to boot. That's a mistake, as network protocols almost always use big-endian.
> So what is the range of values for the length, and how are they actually encoded? Are they uint32 in native binary format? And are you permitted to change the encoding, to fix it? (If it were my choice, I'd make it a printable decimal value, first choice, or printable hex, second choice.)

They are ASCII stream, actually JSON with the exception of the first 4 bytes. I avoided using struct class for this as it's overkill for my application.

So the idea is that i code the length to a max of 0xff (max length of 256 bytes), I would only have to assign 4 bytes to it. If i need 1k length, i just need to increase it to 6 bytes or 4 if i decide to strip the 0x.

the code for the function is as follows:

def request_get_session(sock, jmsg):
# append message length
plen = hex(len(jmsg))
msg = '{0}{1}'.format(plen, jmsg)

print 'sending data'
n = sock.send(msg)
str = '{0} bytes sent: {1}'.format(n, msg)
print str

# receive message length
print 'receiving data'
mlen = sock.recv(4)
nbuf = int(mlen, 16)
except ValueError as e:
print 'invalid length type'
return -1

print 'message length is {0}'.format(nbuf)

while True:
buf = sock.recv(nbuf)

if not buf:

slen = len(buf)
str = "{0} bytes received: {1}".format(slen, buf)
print str
return 0
>> I've managed to receive and translate the message length until I reach my second recv which I readjusted the buffer size to include the new message length.
>> However that failed and recv received 0 bytes. I implemented the same algorithm on the server side using C and it work so appreciate if you can help me on this.
>> # receive message length
>> print 'receiving data'
>> mlen = sock.recv(4)
>> try:
>> nbuf = int(mlen, 16)

> That supposes the count is being sent as a printable string of hex digits. That's not what I concluded above.

That is what I meant. it just an ascii string.

All times are GMT. The time now is 09:45 AM.

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