Created
September 8, 2015 16:05
-
-
Save 1st1/90f052171f062097d1bc to your computer and use it in GitHub Desktop.
a quick example of new async/await in python 3.5
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
| import asyncio | |
| async def http_get(domain): | |
| reader, writer = await asyncio.open_connection(domain, 80) | |
| writer.write(b'\r\n'.join([ | |
| b'GET / HTTP/1.1', | |
| b'Host: %b' % domain.encode('latin-1'), | |
| b'Connection: close', | |
| b'', b'' | |
| ])) | |
| async for line in reader: | |
| print('>>>', line) | |
| writer.close() | |
| loop = asyncio.get_event_loop() | |
| try: | |
| loop.run_until_complete(http_get('example.com')) | |
| finally: | |
| loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment