Created
March 11, 2011 00:14
-
-
Save progrium/865232 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """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