Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'subject:Python': 0.05; 'bytes.': 0.07; 'chunk': 0.07; 'try:': 0.07; 'python': 0.09; '16)': 0.09; "b''": 0.09; 'length.': 0.09; 'valueerror': 0.09; 'chunk:': 0.16; 'closed:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'byte': 0.17; 'bytes': 0.17; 'implementing': 0.17; 'specify': 0.17; '(in': 0.18; 'translate': 0.20; "i've": 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'implemented': 0.27; 'i.e.': 0.27; 'coded': 0.29; 'str': 0.29; 'this.': 0.29; "i'm": 0.29; 'connection': 0.30; 'server.': 0.32; 'print': 0.32; 'to:addr :python-list': 0.33; 'hi,': 0.33; 'server': 0.35; 'except': 0.36; 'skip:{ 10': 0.36; 'client': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'first': 0.61; 'side': 0.61; 'received:': 0.62; 'stuck': 0.65; 'header:Reply-To:1': 0.68; 'receive': 0.71; 'reply-to:no real name:2**0': 0.72; 'payload': 0.84; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=XeZXOvF5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=Tv4_tr9OjFAA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=xZa4izBSp3IA:10 a=pW-UGsLmY2f97FqfLsgA:9 a=wPNLvfGTeEIA:10 a=xiBW_ZDfI7jD5jAx:21 a=7P_hauJaCPGJNdLe:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Mon, 11 Feb 2013 01:55:00 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python recv loop References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360547693 news.xs4all.nl 6915 [2001:888:2000:d::a6]:45666 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38635 On 2013-02-11 00:48, 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{...} > > 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 > You should keep reading until you get all need or the connection is closed: buf = b'' while len(buf) < nbuf: chunk = sock.recv(nbuf - len(buf)) if not chunk: break buf += chunk