Learning python networking

Chris Angelico rosuav at gmail.com
Wed Jan 15 09:07:51 EST 2014


On Thu, Jan 16, 2014 at 12:31 AM, Frank Millman <frank at chagford.com> 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



More information about the Python-list mailing list