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