"Chris Angelico" <[email protected]> wrote in message
news:CAPTjJmpb6yr-VpWypbJQn0a=pnjvnv2cchvbzak+v_5josq...@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
--
https://mail.python.org/mailman/listinfo/python-list