Skip to content

Instantly share code, notes, and snippets.

@SpComb
Created September 24, 2014 10:38
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 SpComb/4f6ebb0cccc17b224fd4 to your computer and use it in GitHub Desktop.
Save SpComb/4f6ebb0cccc17b224fd4 to your computer and use it in GitHub Desktop.
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