Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63985

Re: Learning python networking

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; "'')": 0.09; 'data:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'wrote': 0.14; "b''": 0.16; 'guilty': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sentinel': 0.16; 'skip:n 70': 0.16; 'subject:python': 0.16; 'bit': 0.19; 'this:': 0.26; 'second': 0.26; 'header:X-Complaints- To:1': 0.27; 'omitted': 0.31; 'run': 0.32; 'received:co.za': 0.34; 'received:za': 0.34; 'but': 0.35; 'there': 0.35; 'should': 0.36; 'handle': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'frank': 0.68; 'line,': 0.68; 'received:41': 0.70
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From "Frank Millman" <frank@chagford.com>
Subject Re: Learning python networking
Date Wed, 15 Jan 2014 15:31:15 +0200
References <22d58d76-f2c2-4a1d-8049-3409ac4665d3@googlegroups.com> <mailman.5207.1389223748.18130.python-list@python.org> <9202d352-e065-4f2b-a9e0-e29ce5c68df6@googlegroups.com> <CAPTjJmpb6yr-VpWypbJQn0a=pNjvNV2CchVBZaK+v_5JoSQOBg@mail.gmail.com>
X-Gmane-NNTP-Posting-Host 41-133-114-199.dsl.mweb.co.za
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.3790.4657
X-RFC2646 Format=Flowed; Original
X-MimeOLE Produced By Microsoft MimeOLE V6.00.3790.4913
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5512.1389792688.18130.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389792688 news.xs4all.nl 2832 [2001:888:2000:d::a6]:43394
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63985

Show key headers only | View raw


"Chris Angelico" <rosuav@gmail.com> wrote in message 
news:CAPTjJmpb6yr-VpWypbJQn0a=pNjvNV2CchVBZaK+v_5JoSQOBg@mail.gmail.com...
> You just run a loop like this:
>
> buffer = b''
>
> 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
>    line, buffer = buffer.split(b'\n',1)
>    return line.decode().replace('\r', '')
>

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', '')

Also, as I am looking at it, I notice that the second line should say -

    while b'\n' not in buffer:

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.

Frank Millman


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Learning python networking Paul Pittlerson <menkomigen6@gmail.com> - 2014-01-08 14:27 -0800
  Re: Learning python networking Chris Angelico <rosuav@gmail.com> - 2014-01-09 10:29 +1100
    Re: Learning python networking Paul Pittlerson <menkomigen6@gmail.com> - 2014-01-15 02:37 -0800
      Re: Learning python networking Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-15 11:11 +0000
      Re: Learning python networking Chris Angelico <rosuav@gmail.com> - 2014-01-15 23:52 +1100
      Re: Learning python networking "Frank Millman" <frank@chagford.com> - 2014-01-15 15:31 +0200
      Re: Learning python networking Chris Angelico <rosuav@gmail.com> - 2014-01-16 01:07 +1100
      Re: Learning python networking Chris Angelico <rosuav@gmail.com> - 2014-01-16 02:36 +1100
      Re: Learning python networking Chris Angelico <rosuav@gmail.com> - 2014-01-16 03:31 +1100
      Re: Learning python networking Chris Angelico <rosuav@gmail.com> - 2014-01-16 03:32 +1100
      Re: Learning python networking William Ray Wing <wrw@mac.com> - 2014-01-15 11:43 -0500
      Re: Learning python networking Chris Angelico <rosuav@gmail.com> - 2014-01-16 04:07 +1100
      Re: Learning python networking William Ray Wing <wrw@mac.com> - 2014-01-15 11:25 -0500
  Re: Learning python networking Dan Stromberg <drsalists@gmail.com> - 2014-01-08 15:53 -0800
  Re: Learning python networking Chris Angelico <rosuav@gmail.com> - 2014-01-09 11:07 +1100
  Re: Learning python networking Dan Stromberg <drsalists@gmail.com> - 2014-01-08 19:49 -0800
    Re: Learning python networking Alister <alister.ware@ntlworld.com> - 2014-01-09 11:32 +0000
  Re: Learning python networking Chris Angelico <rosuav@gmail.com> - 2014-01-09 15:20 +1100

csiph-web