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