Files
deb-python-trollius/examples/fetch0.py
Victor Stinner 684f3be000 Python issue #23208: Add BaseEventLoop._current_handle
In debug mode, BaseEventLoop._run_once() now sets the
BaseEventLoop._current_handle attribute to the handle currently executed.
In release mode or when no handle is executed, the attribute is None.

BaseEventLoop.default_exception_handler() displays the traceback of the current
handle if available.
2015-01-26 10:52:45 +01:00

36 lines
745 B
Python

"""Simplest possible HTTP client."""
import sys
from asyncio import *
@coroutine
def fetch():
r, w = yield from open_connection('python.org', 80)
request = 'GET / HTTP/1.0\r\n\r\n'
print('>', request, file=sys.stderr)
w.write(request.encode('latin-1'))
while True:
line = yield from r.readline()
line = line.decode('latin-1').rstrip()
if not line:
break
print('<', line, file=sys.stderr)
print(file=sys.stderr)
body = yield from r.read()
return body
def main():
loop = get_event_loop()
try:
body = loop.run_until_complete(fetch())
finally:
loop.close()
print(body.decode('latin-1'), end='')
if __name__ == '__main__':
main()