Skip to content

Instantly share code, notes, and snippets.

@progrium
Created March 11, 2011 00:14
Show Gist options
  • Select an option

  • Save progrium/865232 to your computer and use it in GitHub Desktop.

Select an option

Save progrium/865232 to your computer and use it in GitHub Desktop.
"""A line protocol generator
Usage:
import socket
sock = socket.create_connection((host, port))
for line in line_protocol(sock):
print line
sock.send("ack!")
"""
def line_protocol(socket, strip=True):
fileobj = socket.makefile()
while True:
try:
line = fileobj.readline() # returns None on EOF
if line is not None and strip:
line = line.strip()
except IOError:
line = None
if line is not None:
yield line
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment