Re: Python recv loop

Discussion in 'Python' started by Ihsan Junaidi Ibrahim, Feb 11, 2013.

  1. 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.
    Ihsan Junaidi Ibrahim, Feb 11, 2013
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. deepak nayak
    Oct 25, 2007
  2. Ihsan Junaidi Ibrahim

    Python recv loop

    Ihsan Junaidi Ibrahim, Feb 11, 2013, in forum: Python
  3. Dave Angel

    Re: Python recv loop

    Dave Angel, Feb 11, 2013, in forum: Python
    Dave Angel
    Feb 11, 2013
  4. MRAB

    Re: Python recv loop

    MRAB, Feb 11, 2013, in forum: Python
  5. Isaac Won
    Ulrich Eckhardt
    Mar 4, 2013