Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 3 | .. _asyncio-streams: |
Victor Stinner | 4b4f9eb | 2014-01-24 17:33:20 +0100 | [diff] [blame] | 4 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 5 | ======= |
| 6 | Streams |
| 7 | ======= |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 8 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 9 | Streams are high-level async/await-ready primitives to work with |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 10 | network connections. Streams allow sending and receiving data without |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 11 | using callbacks or low-level protocols and transports. |
lf | 627d2c8 | 2017-07-25 17:03:51 -0600 | [diff] [blame] | 12 | |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 13 | .. _asyncio_example_stream: |
| 14 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 15 | Here is an example of a TCP echo client written using asyncio |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 16 | streams:: |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 17 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 18 | import asyncio |
Guido van Rossum | 19ff697 | 2015-10-19 13:18:04 -0700 | [diff] [blame] | 19 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 20 | async def tcp_echo_client(message): |
| 21 | reader, writer = await asyncio.open_connection( |
| 22 | '127.0.0.1', 8888) |
| 23 | |
| 24 | print(f'Send: {message!r}') |
Andrew Svetlov | 11194c8 | 2018-09-13 16:53:49 -0700 | [diff] [blame] | 25 | await writer.awrite(message.encode()) |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 26 | |
| 27 | data = await reader.read(100) |
| 28 | print(f'Received: {data.decode()!r}') |
| 29 | |
| 30 | print('Close the connection') |
Andrew Svetlov | 11194c8 | 2018-09-13 16:53:49 -0700 | [diff] [blame] | 31 | await writer.aclose() |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 32 | |
| 33 | asyncio.run(tcp_echo_client('Hello World!')) |
Guido van Rossum | 19ff697 | 2015-10-19 13:18:04 -0700 | [diff] [blame] | 34 | |
| 35 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 36 | See also the `Examples`_ section below. |
| 37 | |
| 38 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 39 | .. rubric:: Stream Functions |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 40 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 41 | The following top-level asyncio functions can be used to create |
| 42 | and work with streams: |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 43 | |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 44 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 45 | .. coroutinefunction:: open_connection(host=None, port=None, \*, \ |
| 46 | loop=None, limit=None, ssl=None, family=0, \ |
| 47 | proto=0, flags=0, sock=None, local_addr=None, \ |
| 48 | server_hostname=None, ssl_handshake_timeout=None) |
| 49 | |
| 50 | Establish a network connection and return a pair of |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 51 | ``(reader, writer)`` objects. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 52 | |
| 53 | The returned *reader* and *writer* objects are instances of |
| 54 | :class:`StreamReader` and :class:`StreamWriter` classes. |
| 55 | |
| 56 | The *loop* argument is optional and can always be determined |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 57 | automatically when this function is awaited from a coroutine. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 58 | |
| 59 | *limit* determines the buffer size limit used by the |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 60 | returned :class:`StreamReader` instance. By default the *limit* |
| 61 | is set to 64 KiB. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 62 | |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 63 | The rest of the arguments are passed directly to |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 64 | :meth:`loop.create_connection`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 65 | |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 66 | .. versionadded:: 3.7 |
| 67 | |
| 68 | The *ssl_handshake_timeout* parameter. |
| 69 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 70 | .. coroutinefunction:: start_server(client_connected_cb, host=None, \ |
| 71 | port=None, \*, loop=None, limit=None, \ |
| 72 | family=socket.AF_UNSPEC, \ |
| 73 | flags=socket.AI_PASSIVE, sock=None, \ |
| 74 | backlog=100, ssl=None, reuse_address=None, \ |
| 75 | reuse_port=None, ssl_handshake_timeout=None, \ |
| 76 | start_serving=True) |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 77 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 78 | Start a socket server. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 79 | |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 80 | The *client_connected_cb* callback is called whenever a new client |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 81 | connection is established. It receives a ``(reader, writer)`` pair |
| 82 | as two arguments, instances of the :class:`StreamReader` and |
| 83 | :class:`StreamWriter` classes. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 84 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 85 | *client_connected_cb* can be a plain callable or a |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 86 | :ref:`coroutine function <coroutine>`; if it is a coroutine function, |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 87 | it will be automatically scheduled as a :class:`Task`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 88 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 89 | The *loop* argument is optional and can always be determined |
| 90 | automatically when this method is awaited from a coroutine. |
| 91 | |
| 92 | *limit* determines the buffer size limit used by the |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 93 | returned :class:`StreamReader` instance. By default the *limit* |
| 94 | is set to 64 KiB. |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 95 | |
| 96 | The rest of the arguments are passed directly to |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 97 | :meth:`loop.create_server`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 98 | |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 99 | .. versionadded:: 3.7 |
| 100 | |
| 101 | The *ssl_handshake_timeout* and *start_serving* parameters. |
| 102 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 103 | |
| 104 | .. rubric:: Unix Sockets |
| 105 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 106 | .. coroutinefunction:: open_unix_connection(path=None, \*, loop=None, \ |
| 107 | limit=None, ssl=None, sock=None, \ |
| 108 | server_hostname=None, ssl_handshake_timeout=None) |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 109 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 110 | Establish a Unix socket connection and return a pair of |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 111 | ``(reader, writer)``. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 112 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 113 | Similar to :func:`open_connection` but operates on Unix sockets. |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 114 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 115 | See also the documentation of :meth:`loop.create_unix_connection`. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 116 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame^] | 117 | .. availability:: Unix. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 118 | |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 119 | .. versionadded:: 3.7 |
| 120 | |
| 121 | The *ssl_handshake_timeout* parameter. |
| 122 | |
| 123 | .. versionchanged:: 3.7 |
| 124 | |
| 125 | The *path* parameter can now be a :term:`path-like object` |
| 126 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 127 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 128 | .. coroutinefunction:: start_unix_server(client_connected_cb, path=None, \ |
| 129 | \*, loop=None, limit=None, sock=None, \ |
| 130 | backlog=100, ssl=None, ssl_handshake_timeout=None, \ |
| 131 | start_serving=True) |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 132 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 133 | Start a Unix socket server. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 134 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 135 | Similar to :func:`start_server` but works with Unix sockets. |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 136 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 137 | See also the documentation of :meth:`loop.create_unix_server`. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 138 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame^] | 139 | .. availability:: Unix. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 140 | |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 141 | .. versionadded:: 3.7 |
| 142 | |
| 143 | The *ssl_handshake_timeout* and *start_serving* parameters. |
| 144 | |
| 145 | .. versionchanged:: 3.7 |
| 146 | |
| 147 | The *path* parameter can now be a :term:`path-like object`. |
| 148 | |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 149 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 150 | --------- |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 151 | |
| 152 | |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 153 | StreamReader |
| 154 | ============ |
| 155 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 156 | .. class:: StreamReader |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 157 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 158 | Represents a reader object that provides APIs to read data |
| 159 | from the IO stream. |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 160 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 161 | It is not recommended to instantiate *StreamReader* objects |
| 162 | directly; use :func:`open_connection` and :func:`start_server` |
| 163 | instead. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 164 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 165 | .. coroutinemethod:: read(n=-1) |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 166 | |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 167 | Read up to *n* bytes. If *n* is not provided, or set to ``-1``, |
| 168 | read until EOF and return all read bytes. |
| 169 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 170 | If EOF was received and the internal buffer is empty, |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 171 | return an empty ``bytes`` object. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 172 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 173 | .. coroutinemethod:: readline() |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 174 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 175 | Read one line, where "line" is a sequence of bytes |
| 176 | ending with ``\n``. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 177 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 178 | If EOF is received and ``\n`` was not found, the method |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 179 | returns partially read data. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 180 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 181 | If EOF is received and the internal buffer is empty, |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 182 | return an empty ``bytes`` object. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 183 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 184 | .. coroutinemethod:: readexactly(n) |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 185 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 186 | Read exactly *n* bytes. |
| 187 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 188 | Raise an :exc:`IncompleteReadError` if EOF is reached before *n* |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 189 | can be read. Use the :attr:`IncompleteReadError.partial` |
| 190 | attribute to get the partially read data. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 191 | |
Berker Peksag | e5b0bd1 | 2016-10-18 00:34:46 +0300 | [diff] [blame] | 192 | .. coroutinemethod:: readuntil(separator=b'\\n') |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 193 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 194 | Read data from the stream until *separator* is found. |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 195 | |
| 196 | On success, the data and separator will be removed from the |
| 197 | internal buffer (consumed). Returned data will include the |
| 198 | separator at the end. |
| 199 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 200 | If the amount of data read exceeds the configured stream limit, a |
| 201 | :exc:`LimitOverrunError` exception is raised, and the data |
| 202 | is left in the internal buffer and can be read again. |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 203 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 204 | If EOF is reached before the complete separator is found, |
| 205 | an :exc:`IncompleteReadError` exception is raised, and the internal |
| 206 | buffer is reset. The :attr:`IncompleteReadError.partial` attribute |
| 207 | may contain a portion of the separator. |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 208 | |
| 209 | .. versionadded:: 3.5.2 |
| 210 | |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 211 | .. method:: at_eof() |
| 212 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 213 | Return ``True`` if the buffer is empty and :meth:`feed_eof` |
| 214 | was called. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 215 | |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 216 | |
| 217 | StreamWriter |
| 218 | ============ |
| 219 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 220 | .. class:: StreamWriter |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 221 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 222 | Represents a writer object that provides APIs to write data |
| 223 | to the IO stream. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 224 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 225 | It is not recommended to instantiate *StreamWriter* objects |
| 226 | directly; use :func:`open_connection` and :func:`start_server` |
| 227 | instead. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 228 | |
Andrew Svetlov | 11194c8 | 2018-09-13 16:53:49 -0700 | [diff] [blame] | 229 | .. coroutinemethod:: awrite(data) |
| 230 | |
| 231 | Write *data* to the stream. |
| 232 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 233 | The method respects flow control, execution is paused if the write |
| 234 | buffer reaches the high watermark. |
Andrew Svetlov | 11194c8 | 2018-09-13 16:53:49 -0700 | [diff] [blame] | 235 | |
| 236 | .. versionadded:: 3.8 |
| 237 | |
| 238 | .. coroutinemethod:: aclose() |
| 239 | |
| 240 | Close the stream. |
| 241 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 242 | Wait until all closing actions are complete, e.g. SSL shutdown for |
Andrew Svetlov | 11194c8 | 2018-09-13 16:53:49 -0700 | [diff] [blame] | 243 | secure sockets. |
| 244 | |
| 245 | .. versionadded:: 3.8 |
| 246 | |
| 247 | .. method:: can_write_eof() |
| 248 | |
| 249 | Return *True* if the underlying transport supports |
| 250 | the :meth:`write_eof` method, *False* otherwise. |
| 251 | |
| 252 | .. method:: write_eof() |
| 253 | |
| 254 | Close the write end of the stream after the buffered write |
| 255 | data is flushed. |
| 256 | |
| 257 | .. attribute:: transport |
| 258 | |
| 259 | Return the underlying asyncio transport. |
| 260 | |
| 261 | .. method:: get_extra_info(name, default=None) |
| 262 | |
| 263 | Access optional transport information; see |
| 264 | :meth:`BaseTransport.get_extra_info` for details. |
| 265 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 266 | .. method:: write(data) |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 267 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 268 | Write *data* to the stream. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 269 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 270 | This method is not subject to flow control. Calls to ``write()`` should |
| 271 | be followed by :meth:`drain`. The :meth:`awrite` method is a |
| 272 | recommended alternative the applies flow control automatically. |
Andrew Svetlov | 11194c8 | 2018-09-13 16:53:49 -0700 | [diff] [blame] | 273 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 274 | .. method:: writelines(data) |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 275 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 276 | Write a list (or any iterable) of bytes to the stream. |
Victor Stinner | ffbe3c6 | 2014-02-08 22:50:07 +0100 | [diff] [blame] | 277 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 278 | This method is not subject to flow control. Calls to ``writelines()`` |
| 279 | should be followed by :meth:`drain`. |
Andrew Svetlov | 11194c8 | 2018-09-13 16:53:49 -0700 | [diff] [blame] | 280 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 281 | .. coroutinemethod:: drain() |
| 282 | |
| 283 | Wait until it is appropriate to resume writing to the stream. |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 284 | Example:: |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 285 | |
| 286 | writer.write(data) |
| 287 | await writer.drain() |
| 288 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 289 | This is a flow control method that interacts with the underlying |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 290 | IO write buffer. When the size of the buffer reaches |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 291 | the high watermark, *drain()* blocks until the size of the |
| 292 | buffer is drained down to the low watermark and writing can |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 293 | be resumed. When there is nothing to wait for, the :meth:`drain` |
| 294 | returns immediately. |
Victor Stinner | ffbe3c6 | 2014-02-08 22:50:07 +0100 | [diff] [blame] | 295 | |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 296 | .. method:: close() |
| 297 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 298 | Close the stream. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 299 | |
Andrew Svetlov | fe133aa | 2018-01-25 00:30:30 +0200 | [diff] [blame] | 300 | .. method:: is_closing() |
| 301 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 302 | Return ``True`` if the stream is closed or in the process of |
| 303 | being closed. |
Andrew Svetlov | fe133aa | 2018-01-25 00:30:30 +0200 | [diff] [blame] | 304 | |
| 305 | .. versionadded:: 3.7 |
| 306 | |
| 307 | .. coroutinemethod:: wait_closed() |
| 308 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 309 | Wait until the stream is closed. |
Andrew Svetlov | fe133aa | 2018-01-25 00:30:30 +0200 | [diff] [blame] | 310 | |
Yury Selivanov | 8be876e | 2018-09-11 17:10:37 -0700 | [diff] [blame] | 311 | Should be called after :meth:`close` to wait until the underlying |
| 312 | connection is closed. |
Andrew Svetlov | fe133aa | 2018-01-25 00:30:30 +0200 | [diff] [blame] | 313 | |
| 314 | .. versionadded:: 3.7 |
| 315 | |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 316 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 317 | Examples |
| 318 | ======== |
Victor Stinner | 5121a9b | 2014-10-11 15:52:14 +0200 | [diff] [blame] | 319 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 320 | .. _asyncio-tcp-echo-client-streams: |
| 321 | |
| 322 | TCP echo client using streams |
| 323 | ----------------------------- |
| 324 | |
| 325 | TCP echo client using the :func:`asyncio.open_connection` function:: |
| 326 | |
| 327 | import asyncio |
| 328 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 329 | async def tcp_echo_client(message): |
| 330 | reader, writer = await asyncio.open_connection( |
| 331 | '127.0.0.1', 8888) |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 332 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 333 | print(f'Send: {message!r}') |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 334 | writer.write(message.encode()) |
| 335 | |
Andrew Svetlov | 8874342 | 2017-12-11 17:35:49 +0200 | [diff] [blame] | 336 | data = await reader.read(100) |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 337 | print(f'Received: {data.decode()!r}') |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 338 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 339 | print('Close the connection') |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 340 | writer.close() |
| 341 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 342 | asyncio.run(tcp_echo_client('Hello World!')) |
| 343 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 344 | |
| 345 | .. seealso:: |
| 346 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 347 | The :ref:`TCP echo client protocol <asyncio_example_tcp_echo_client_protocol>` |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 348 | example uses the low-level :meth:`loop.create_connection` method. |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 349 | |
| 350 | |
| 351 | .. _asyncio-tcp-echo-server-streams: |
| 352 | |
| 353 | TCP echo server using streams |
| 354 | ----------------------------- |
| 355 | |
| 356 | TCP echo server using the :func:`asyncio.start_server` function:: |
| 357 | |
| 358 | import asyncio |
| 359 | |
Andrew Svetlov | 8874342 | 2017-12-11 17:35:49 +0200 | [diff] [blame] | 360 | async def handle_echo(reader, writer): |
| 361 | data = await reader.read(100) |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 362 | message = data.decode() |
| 363 | addr = writer.get_extra_info('peername') |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 364 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 365 | print(f"Received {message!r} from {addr!r}") |
| 366 | |
| 367 | print(f"Send: {message!r}") |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 368 | writer.write(data) |
Andrew Svetlov | 8874342 | 2017-12-11 17:35:49 +0200 | [diff] [blame] | 369 | await writer.drain() |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 370 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 371 | print("Close the connection") |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 372 | writer.close() |
| 373 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 374 | async def main(): |
| 375 | server = await asyncio.start_server( |
| 376 | handle_echo, '127.0.0.1', 8888) |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 377 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 378 | addr = server.sockets[0].getsockname() |
| 379 | print(f'Serving on {addr}') |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 380 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 381 | async with server: |
| 382 | await server.serve_forever() |
| 383 | |
| 384 | asyncio.run(main()) |
| 385 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 386 | |
| 387 | .. seealso:: |
| 388 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 389 | The :ref:`TCP echo server protocol <asyncio_example_tcp_echo_server_protocol>` |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 390 | example uses the :meth:`loop.create_server` method. |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 391 | |
| 392 | |
Victor Stinner | 5121a9b | 2014-10-11 15:52:14 +0200 | [diff] [blame] | 393 | Get HTTP headers |
| 394 | ---------------- |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 395 | |
| 396 | Simple example querying HTTP headers of the URL passed on the command line:: |
| 397 | |
| 398 | import asyncio |
| 399 | import urllib.parse |
| 400 | import sys |
| 401 | |
Mikhail Terekhov | d2ac400 | 2018-08-07 16:29:06 -0400 | [diff] [blame] | 402 | async def print_http_headers(url): |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 403 | url = urllib.parse.urlsplit(url) |
Victor Stinner | 5121a9b | 2014-10-11 15:52:14 +0200 | [diff] [blame] | 404 | if url.scheme == 'https': |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 405 | reader, writer = await asyncio.open_connection( |
| 406 | url.hostname, 443, ssl=True) |
Victor Stinner | 5121a9b | 2014-10-11 15:52:14 +0200 | [diff] [blame] | 407 | else: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 408 | reader, writer = await asyncio.open_connection( |
| 409 | url.hostname, 80) |
| 410 | |
| 411 | query = ( |
| 412 | f"HEAD {url.path or '/'} HTTP/1.0\r\n" |
| 413 | f"Host: {url.hostname}\r\n" |
| 414 | f"\r\n" |
| 415 | ) |
| 416 | |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 417 | writer.write(query.encode('latin-1')) |
| 418 | while True: |
Andrew Svetlov | 8874342 | 2017-12-11 17:35:49 +0200 | [diff] [blame] | 419 | line = await reader.readline() |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 420 | if not line: |
| 421 | break |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 422 | |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 423 | line = line.decode('latin1').rstrip() |
| 424 | if line: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 425 | print(f'HTTP header> {line}') |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 426 | |
Victor Stinner | 5121a9b | 2014-10-11 15:52:14 +0200 | [diff] [blame] | 427 | # Ignore the body, close the socket |
| 428 | writer.close() |
| 429 | |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 430 | url = sys.argv[1] |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 431 | asyncio.run(print_http_headers(url)) |
| 432 | |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 433 | |
| 434 | Usage:: |
| 435 | |
| 436 | python example.py http://example.com/path/page.html |
| 437 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 438 | or with HTTPS:: |
| 439 | |
| 440 | python example.py https://example.com/path/page.html |
| 441 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 442 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 443 | .. _asyncio_example_create_connection-streams: |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 444 | |
| 445 | Register an open socket to wait for data using streams |
| 446 | ------------------------------------------------------ |
| 447 | |
| 448 | Coroutine waiting until a socket receives data using the |
| 449 | :func:`open_connection` function:: |
| 450 | |
| 451 | import asyncio |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 452 | import socket |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 453 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 454 | async def wait_for_data(): |
| 455 | # Get a reference to the current event loop because |
| 456 | # we want to access low-level APIs. |
| 457 | loop = asyncio.get_running_loop() |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 458 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 459 | # Create a pair of connected sockets. |
| 460 | rsock, wsock = socket.socketpair() |
| 461 | |
| 462 | # Register the open socket to wait for data. |
| 463 | reader, writer = await asyncio.open_connection(sock=rsock) |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 464 | |
| 465 | # Simulate the reception of data from the network |
| 466 | loop.call_soon(wsock.send, 'abc'.encode()) |
| 467 | |
| 468 | # Wait for data |
Andrew Svetlov | 8874342 | 2017-12-11 17:35:49 +0200 | [diff] [blame] | 469 | data = await reader.read(100) |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 470 | |
| 471 | # Got data, we are done: close the socket |
| 472 | print("Received:", data.decode()) |
| 473 | writer.close() |
| 474 | |
| 475 | # Close the second socket |
| 476 | wsock.close() |
| 477 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 478 | asyncio.run(wait_for_data()) |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 479 | |
| 480 | .. seealso:: |
| 481 | |
| 482 | The :ref:`register an open socket to wait for data using a protocol |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 483 | <asyncio_example_create_connection>` example uses a low-level protocol and |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 484 | the :meth:`loop.create_connection` method. |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 485 | |
| 486 | The :ref:`watch a file descriptor for read events |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 487 | <asyncio_example_watch_fd>` example uses the low-level |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 488 | :meth:`loop.add_reader` method to watch a file descriptor. |