Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:Python': 0.05; 'cc:addr:python-list': 0.10; 'assume': 0.11; '"\\n":': 0.16; 'buffering': 0.16; 'ends,': 0.16; 'expecting': 0.16; 'fits': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:Accessing': 0.16; 'tcp': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'alternate': 0.18; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'parsing': 0.22; 'am,': 0.23; '2015': 0.23; 'sat,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'said,': 0.27; 'message-id:@mail.gmail.com': 0.28; 'this.': 0.28; 'boundaries': 0.29; 'termination': 0.29; 'read,': 0.29; 'that.': 0.30; 'writes': 0.31; 'operations': 0.31; 'code': 0.31; "can't": 0.32; 'though,': 0.32; 'usually': 0.33; 'case,': 0.34; 'running': 0.34; 'received:google.com': 0.34; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'smaller': 0.36; 'two': 0.37; 'subject:: ': 0.37; 'stuff': 0.38; 'end': 0.39; 'data': 0.40; 'mark': 0.40; 'subject:with': 0.40; 'your': 0.60; '30,': 0.63; 'chrisa': 0.84; 'to:none': 0.90; 'edwards': 0.91; 'subject:Server': 0.91; 'period.': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=U1rUbdEMkFgmvHiaidGc88VWIrduB59LRPG5EnfFizM=; b=ptHVUzDLQLfZ8J0k2YLR02Mms6CWqL8E2QA42coJbNUhRq9xh/rYe/gyxCO3R4nLT2 SwpM2MXEIrIV010/V94A94DtgVpztO6Br3xs+EK47kwuB/44itCxClyELAWzb6KEfhQk B2OupFc1rB0M/jpGFfdD7rTxi1gd35f4at1rzaryMZ7EQlivjDlmEikEUQClIzJvAdEs JjJWDH8Qy4gmHYWyijF+cop8RRGyG77sfphYY+LSrJuHo8vtfLcKIzUkeV3O9WBfyHpB IjQtGjKNYmzXSgylyf3nyqxLdnv9lvVc/u20kuBTvlB1saGenSu8jlJusVQbRlHXp+7N bFAg== MIME-Version: 1.0 X-Received: by 10.107.16.149 with SMTP id 21mr11235186ioq.53.1432917606643; Fri, 29 May 2015 09:40:06 -0700 (PDT) In-Reply-To: References: Date: Sat, 30 May 2015 02:40:06 +1000 Subject: Re: Accessing DataSocket Server with Python From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432917609 news.xs4all.nl 2935 [2001:888:2000:d::a6]:45407 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91475 On Sat, May 30, 2015 at 2:29 AM, Grant Edwards wrote: > If you assume TCP read/write operations are atomic and "message" > boundaries are preserved, your code is wrong. It will eventually > fail. Period. Indeed. That said, though, if your writes are all smaller than one packet, and you perfectly alternate a write and a read, a write and a read, at both ends, then you can go a very long way without ever running into this. In that case, you could have proper parsing and buffering as a failure case, with the normal case expecting a termination mark at the end of the socket-read - something like this: write(query) data = read(as_much_as_possible) if data[-1] != "\n": # Huh, stuff got split. while True: more_data = read(as_much_as_possible) data += moredata if data[-1] == "\n": break cope_with_possibility_of(socket_disconnection) handle_response(data) Alternating writes and reads ensures that two messages can't be combined, and if a single message fits inside a packet, it'll usually be sent that way. But you still can't guarantee that. ChrisA