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


Groups > comp.lang.python > #38625

Python recv loop

From Ihsan Junaidi Ibrahim <ihsan@grep.my>
Subject Python recv loop
Date 2013-02-11 08:48 +0800
Newsgroups comp.lang.python
Message-ID <mailman.1612.1360544258.2939.python-list@python.org> (permalink)

Show all headers | View raw


Hi,

I'm implementing a python client connecting to a C-backend server and am currently stuck to as to how to proceed with receiving variable-length byte stream coming in from the server.

I have coded the first 4 bytes (in hexadecimal) of message coming in from the server to specify the length of the message payload i.e. 0xad{...}

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)
    except ValueError as e:
        print 'invalid length type'
        return -1

    while True:
        buf = sock.recv(nbuf)

        if not buf:
            break

    slen = len(buf)
    str = "{0} bytes received: {1}".format(slen, buf)
    print str

Back to comp.lang.python | Previous | NextNext 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