Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Last active November 17, 2020 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pervognsen/e28b9a5af85782e9a7976b6996a76303 to your computer and use it in GitHub Desktop.
Save pervognsen/e28b9a5af85782e9a7976b6996a76303 to your computer and use it in GitHub Desktop.
buffer = b'' # This is the partial line buffer.
while 1:
buffer += socket.recv(1024) # Read up to 1024 bytes and append it to the buffer.
lines = buffer.split(b'\n') # Split the buffer by newlines.
buffer = lines[-1] # The last piece is a partial line.
for line in lines[:-1]: # Every other piece is a complete line.
process(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment