Learning python networking
Frank Millman
frank at chagford.com
Wed Jan 15 08:31:15 EST 2014
"Chris Angelico" <rosuav at gmail.com> wrote in message
news:CAPTjJmpb6yr-VpWypbJQn0a=pNjvNV2CchVBZaK+v_5JoSQOBg at 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
More information about the Python-list
mailing list