Created
September 24, 2014 10:38
-
-
Save SpComb/4f6ebb0cccc17b224fd4 to your computer and use it in GitHub Desktop.
This file contains 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
class TCPStream: | |
... | |
def __iter__ (self): | |
""" | |
Read lines of input from the socket. | |
Yields strings without trailing newlines. | |
Returns on EOF. | |
""" | |
while True: | |
try: | |
buf = yield from self.readline() | |
except EOFError: | |
return | |
line = buf.rstrip(b'\n').decode(self.encoding) | |
# XXX: crazy abuse | |
yield line | |
@asyncio.coroutine | |
def client (tcp_stream): | |
""" | |
tcp_stream: TCPStream | |
""" | |
for line in tcp_stream: | |
if isinstance(line, asyncio.Future): | |
log.debug("%s: yield ->", tcp_stream) | |
yield from line | |
log.debug("%s: yield <-", tcp_stream) | |
continue | |
log.info("%s: %s", tcp_stream, line) | |
log.info("%s: eof", tcp_stream) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment