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


Groups > comp.lang.python > #38627 > unrolled thread

Re: Python recv loop

Started byDave Angel <davea@davea.name>
First post2013-02-10 20:22 -0500
Last post2013-02-10 20:22 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python recv loop Dave Angel <davea@davea.name> - 2013-02-10 20:22 -0500

#38627 — Re: Python recv loop

FromDave Angel <davea@davea.name>
Date2013-02-10 20:22 -0500
SubjectRe: Python recv loop
Message-ID<mailman.1614.1360545756.2939.python-list@python.org>
On 02/10/2013 07:48 PM, Ihsan Junaidi Ibrahim wrote:
> 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{...}

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.)

>
> 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.

>      except ValueError as e:

If the count starts as 0xad, I can't imagine why this exception wasn't 
thrown.

>          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
>

You might need to play with hexlify, if you persist in sending the count 
in binary.

-- 
DaveA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web