Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'exception': 0.03; 'value,': 0.03; 'string.': 0.04; 'subject:Python': 0.05; 'binary': 0.05; 'append': 0.07; 'ascii': 0.07; 'assign': 0.07; 'bytes.': 0.07; 'json': 0.07; 'msg': 0.07; 'try:': 0.07; '16)': 0.09; 'byte,': 0.09; 'length.': 0.09; 'msg)': 0.09; 'valueerror': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'binary.': 0.16; 'bytes),': 0.16; 'digits.': 0.16; 'format?': 0.16; 'hex': 0.16; 'length,': 0.16; 'true:': 0.16; 'uint32': 0.16; 'string': 0.17; 'wrote:': 0.17; 'fix': 0.17; 'bytes': 0.17; 'certainly': 0.17; 'feb': 0.19; 'translate': 0.20; 'permitted': 0.22; 'strip': 0.22; 'struct': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header :In-Reply-To:1': 0.25; '(which': 0.26; 'values': 0.26; 'am,': 0.27; 'implemented': 0.27; 'decide': 0.28; 'decimal': 0.29; 'description,': 0.29; 'protocols': 0.29; 'str': 0.29; 'probably': 0.29; 'class': 0.29; 'this.': 0.29; 'function': 0.30; 'code': 0.31; "skip:' 20": 0.32; 'print': 0.32; '11,': 0.33; 'received:172.16': 0.34; 'server': 0.35; 'follows:': 0.35; 'except': 0.36; 'skip:{ 10': 0.36; 'charset:us-ascii': 0.36; 'being': 0.37; 'sent:': 0.37; 'subject:: ': 0.38; 'skip:" 10': 0.40; 'help': 0.40; 'range': 0.60; 'first': 0.61; 'side': 0.61; 'header:Message-Id:1': 0.62; 'received:': 0.62; 'receive': 0.71; 'increase': 0.72; '(max': 0.91; 'concluded': 0.91; 'angel': 0.93; 'received:my': 0.93 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: Python recv loop From: Ihsan Junaidi Ibrahim In-Reply-To: <511847C7.1040008@davea.name> Date: Mon, 11 Feb 2013 23:02:44 +0800 Content-Transfer-Encoding: quoted-printable References: <511847C7.1040008@davea.name> To: Dave Angel X-Mailer: Apple Mail (2.1499) Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360594964 news.xs4all.nl 6982 [2001:888:2000:d::a6]:35044 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38690 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. >=20 > 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 =3D hex(len(jmsg)) msg =3D '{0}{1}'.format(plen, jmsg) print 'sending data' n =3D sock.send(msg) str =3D '{0} bytes sent: {1}'.format(n, msg) print str # receive message length print 'receiving data' mlen =3D sock.recv(4) try: nbuf =3D int(mlen, 16) except ValueError as e: print 'invalid length type' return -1 print 'message length is {0}'.format(nbuf) while True: buf =3D sock.recv(nbuf) if not buf: break slen =3D len(buf) str =3D "{0} bytes received: {1}".format(slen, buf) print str return 0 >=20 >>=20 >> 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. >>=20 >> 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. >>=20 >> # receive message length >> print 'receiving data' >> mlen =3D sock.recv(4) >> try: >> nbuf =3D int(mlen, 16) >=20 > 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.