diff options
| author | Victor Stinner <victor.stinner@gmail.com> | 2014-01-23 11:25:48 +0100 |
|---|---|---|
| committer | Victor Stinner <victor.stinner@gmail.com> | 2014-01-23 11:25:48 +0100 |
| commit | c520edc08b89c7e233dd8f7cf37cf5179268167f (patch) | |
| tree | 668ac94f64d9797923f6041494ffaf8cf432d1c2 /Doc/library/asyncio-stream.rst | |
| parent | 24f8ebf4c59a3cd0395950233b0aa46f267ceb87 (diff) | |
| download | cpython-git-c520edc08b89c7e233dd8f7cf37cf5179268167f.tar.gz | |
asyncio doc: add an client example using streams
Diffstat (limited to 'Doc/library/asyncio-stream.rst')
| -rw-r--r-- | Doc/library/asyncio-stream.rst | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Doc/library/asyncio-stream.rst b/Doc/library/asyncio-stream.rst index 19ac103a66..e457fe58da 100644 --- a/Doc/library/asyncio-stream.rst +++ b/Doc/library/asyncio-stream.rst @@ -205,3 +205,38 @@ StreamReaderProtocol XXX + +Example +======= + +Simple example querying HTTP headers of the URL passed on the command line:: + + import asyncio + import urllib.parse + import sys + + @asyncio.coroutine + def print_http_headers(url): + url = urllib.parse.urlsplit(url) + reader, writer = yield from asyncio.open_connection(url.hostname, 80) + query = ('HEAD {url.path} HTTP/1.0\r\n' + 'Host: {url.hostname}\r\n' + '\r\n').format(url=url) + writer.write(query.encode('latin-1')) + while True: + line = yield from reader.readline() + if not line: + break + line = line.decode('latin1').rstrip() + if line: + print('HTTP header> %s' % line) + + url = sys.argv[1] + loop = asyncio.get_event_loop() + task = asyncio.async(print_http_headers(url)) + loop.run_until_complete(task) + +Usage:: + + python example.py http://example.com/path/page.html + |
