@@ -238,8 +238,11 @@ IncompleteReadError
238238 Read bytes string before the end of stream was reached (:class: `bytes `).
239239
240240
241- Example
242- =======
241+ Stream examples
242+ ===============
243+
244+ Get HTTP headers
245+ ----------------
243246
244247Simple example querying HTTP headers of the URL passed on the command line::
245248
@@ -250,10 +253,14 @@ Simple example querying HTTP headers of the URL passed on the command line::
250253 @asyncio.coroutine
251254 def print_http_headers(url):
252255 url = urllib.parse.urlsplit(url)
253- reader, writer = yield from asyncio.open_connection(url.hostname, 80)
254- query = ('HEAD {url.path} HTTP/1.0\r\n'
255- 'Host: {url.hostname}\r\n'
256- '\r\n').format(url=url)
256+ if url.scheme == 'https':
257+ connect = asyncio.open_connection(url.hostname, 443, ssl=True)
258+ else:
259+ connect = asyncio.open_connection(url.hostname, 80)
260+ reader, writer = yield from connect
261+ query = ('HEAD {path} HTTP/1.0\r\n'
262+ 'Host: {hostname}\r\n'
263+ '\r\n').format(path=url.path or '/', hostname=url.hostname)
257264 writer.write(query.encode('latin-1'))
258265 while True:
259266 line = yield from reader.readline()
@@ -263,6 +270,9 @@ Simple example querying HTTP headers of the URL passed on the command line::
263270 if line:
264271 print('HTTP header> %s' % line)
265272
273+ # Ignore the body, close the socket
274+ writer.close()
275+
266276 url = sys.argv[1]
267277 loop = asyncio.get_event_loop()
268278 task = asyncio.async(print_http_headers(url))
@@ -273,3 +283,53 @@ Usage::
273283
274284 python example.py http://example.com/path/page.html
275285
286+ or with HTTPS::
287+
288+ python example.py https://example.com/path/page.html
289+
290+ .. _asyncio-register-socket-streams :
291+
292+ Register an open socket to wait for data using streams
293+ ------------------------------------------------------
294+
295+ Coroutine waiting until a socket receives data using the
296+ :func: `open_connection ` function::
297+
298+ import asyncio
299+ import socket
300+
301+ def wait_for_data(loop):
302+ # Create a pair of connected sockets
303+ rsock, wsock = socket.socketpair()
304+
305+ # Register the open socket to wait for data
306+ reader, writer = yield from asyncio.open_connection(sock=rsock, loop=loop)
307+
308+ # Simulate the reception of data from the network
309+ loop.call_soon(wsock.send, 'abc'.encode())
310+
311+ # Wait for data
312+ data = yield from reader.read(100)
313+
314+ # Got data, we are done: close the socket
315+ print("Received:", data.decode())
316+ writer.close()
317+
318+ # Close the second socket
319+ wsock.close()
320+
321+ loop = asyncio.get_event_loop()
322+ loop.run_until_complete(wait_for_data(loop))
323+ loop.close()
324+
325+ .. seealso ::
326+
327+ The :ref: `register an open socket to wait for data using a protocol
328+ <asyncio-register-socket>` example uses a low-level protocol created by the
329+ :meth: `BaseEventLoop.create_connection ` method.
330+
331+ The :ref: `watch a file descriptor for read events
332+ <asyncio-watch-read-event>` example uses the low-level
333+ :meth: `BaseEventLoop.add_reader ` method to register the file descriptor of a
334+ socket.
335+
0 commit comments