[Python-checkins] cpython (3.4): asyncio doc: the "Get HTTP headers" example now supports HTTPS

victor.stinner python-checkins at python.org
Sat Oct 11 16:17:31 CEST 2014


https://hg.python.org/cpython/rev/a3526d4398ec
changeset:   92957:a3526d4398ec
branch:      3.4
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sat Oct 11 15:52:14 2014 +0200
summary:
  asyncio doc: the "Get HTTP headers" example now supports HTTPS

files:
  Doc/library/asyncio-stream.rst |  22 ++++++++++++++++------
  1 files changed, 16 insertions(+), 6 deletions(-)


diff --git a/Doc/library/asyncio-stream.rst b/Doc/library/asyncio-stream.rst
--- a/Doc/library/asyncio-stream.rst
+++ b/Doc/library/asyncio-stream.rst
@@ -238,8 +238,11 @@
       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 @@
     @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 @@
             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))

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list