summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-10-11 13:52:14 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-10-11 13:52:14 (GMT)
commit5121a9ba4acf890955d407275a3e5f1955c8b283 (patch)
treeb3369631d4242ebe114a0a949ce63445a108d5c1 /Doc/library
parent4cc05517732f012092e36e3458bcbea11282582c (diff)
downloadcpython-5121a9ba4acf890955d407275a3e5f1955c8b283.zip
cpython-5121a9ba4acf890955d407275a3e5f1955c8b283.tar.gz
cpython-5121a9ba4acf890955d407275a3e5f1955c8b283.tar.bz2
asyncio doc: the "Get HTTP headers" example now supports HTTPS
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/asyncio-stream.rst22
1 files changed, 16 insertions, 6 deletions
diff --git a/Doc/library/asyncio-stream.rst b/Doc/library/asyncio-stream.rst
index a4a997e..19ec935 100644
--- a/Doc/library/asyncio-stream.rst
+++ b/Doc/library/asyncio-stream.rst
@@ -238,8 +238,11 @@ IncompleteReadError
Read bytes string before the end of stream was reached (:class:`bytes`).
-Example
-=======
+Stream examples
+===============
+
+Get HTTP headers
+----------------
Simple example querying HTTP headers of the URL passed on the command line::
@@ -250,10 +253,14 @@ Simple example querying HTTP headers of the URL passed on the command line::
@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)
+ if url.scheme == 'https':
+ connect = asyncio.open_connection(url.hostname, 443, ssl=True)
+ else:
+ connect = asyncio.open_connection(url.hostname, 80)
+ reader, writer = yield from connect
+ query = ('HEAD {path} HTTP/1.0\r\n'
+ 'Host: {hostname}\r\n'
+ '\r\n').format(path=url.path or '/', hostname=url.hostname)
writer.write(query.encode('latin-1'))
while True:
line = yield from reader.readline()
@@ -263,6 +270,9 @@ Simple example querying HTTP headers of the URL passed on the command line::
if line:
print('HTTP header> %s' % line)
+ # Ignore the body, close the socket
+ writer.close()
+
url = sys.argv[1]
loop = asyncio.get_event_loop()
task = asyncio.async(print_http_headers(url))