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