Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '16,': 0.03; 'repository': 0.05; 'correct.': 0.07; "'')": 0.09; 'augmented': 0.09; 'data:': 0.09; 'imply': 0.09; "people's": 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; 'changes': 0.15; 'did,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guilty': 0.16; 'sentinel': 0.16; 'utterly': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'thu,': 0.19; 'thanks.': 0.20; 'code,': 0.22; 'cc:addr:python.org': 0.22; '(or': 0.24; 'cc:2**0': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'especially': 0.30; 'message-id:@mail.gmail.com': 0.30; 'omitted': 0.31; 'moment': 0.34; 'there,': 0.34; "i'd": 0.34; 'something': 0.35; 'no,': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'should': 0.36; 'handle': 0.38; 'does': 0.39; 'sure': 0.39; 'catch': 0.60; 'myself': 0.63; 'frank': 0.68; 'improvements': 0.68; 'line,': 0.68; 'apart': 0.72; 'introduce': 0.78; 'correcting': 0.84; 'fortunately,': 0.84; 'improvement': 0.84; 'offended': 0.84; 'wars': 0.84; 'to:none': 0.92; 'mistakes': 0.93 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=ZSllBWDaz+TJLMiQY7IvVSuijy2iqww21oFiqg2dTZE=; b=Ip05eMYDZmwVRENJ4+DIiYLvuwPBlbSG+41QePGo+cxVlyVYZOTbK6Lg9dVQKV4Fch CkAjTNdQmht0/620mwd1Uc52a1lWlILUEs4Fz6uZaHwXrlPscy7OmABfykIfDtsh+Ztq l/+5O8RRe5/gVPUKX7u6xK2QRjvQBbEuXDZSctCBQ0SkuAUkEhhgPmyBO0XmGmTx7zKK kcSS/Dhbu1LA2DqREbWO6mUOdimtoB17WWN94bGyTEbi8sst4FlgB4V04HIbFzy3F2as xYh+y76QHFUJu1A0+95QQq0OvSgUXtDotnEFBQEwENLp/ROBSfYgBxVXZIN3PD9/KTB6 PNBg== MIME-Version: 1.0 X-Received: by 10.68.183.164 with SMTP id en4mr2842143pbc.169.1389794871988; Wed, 15 Jan 2014 06:07:51 -0800 (PST) In-Reply-To: References: <22d58d76-f2c2-4a1d-8049-3409ac4665d3@googlegroups.com> <9202d352-e065-4f2b-a9e0-e29ce5c68df6@googlegroups.com> Date: Thu, 16 Jan 2014 01:07:51 +1100 Subject: Re: Learning python networking 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.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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389795187 news.xs4all.nl 2972 [2001:888:2000:d::a6]:54607 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63987 On Thu, Jan 16, 2014 at 12:31 AM, Frank Millman wrote: > I think you may have omitted a line there - > > def gets(): > while '\n' not in buffer: > data = sock.recv(1024) > if not data: > # Client is disconnected, handle it gracefully > return None # or some other sentinel > #--> > buffer = buffer + data > #--> > line, buffer = buffer.split(b'\n',1) > return line.decode().replace('\r', '') Yes, indeed I did, thanks. Apart from using augmented assignment, that's exactly what I would have put there, if I'd actually taken a moment to test the code. > Also, as I am looking at it, I notice that the second line should say - > > while b'\n' not in buffer: Right again. Fortunately, Py3 would catch that one with a TypeError. See? This is why you should use Py3. :) > I feel a bit guilty nitpicking, as you have provided a wonderfully > comprehensive answer, but I wanted to make sure the OP did not get confused. No no, nitpicking is exactly what ensures that the end result is correct. If I got offended at you correcting my code, it would imply that I think myself perfect (or at least, that I consider you to be utterly incapable of noticing my errors), which is provably false :) One of the mind-set changes that I had to introduce at work was that people don't own code, the repository does - if you see an improvement to something I wrote, or I see an improvement to something you wrote, they're improvements to be committed, not turf wars to be battled over. Especially on something like this, please *do* catch other people's mistakes :) ChrisA