R David Murray | 6a14381 | 2013-12-20 14:37:39 -0500 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 2 | |
Yury Selivanov | cba0053 | 2015-12-16 21:30:52 -0500 | [diff] [blame] | 3 | ++++++++++++++++++++++++++++++++++++++++++++++ |
| 4 | Transports and protocols (callback based API) |
| 5 | ++++++++++++++++++++++++++++++++++++++++++++++ |
Victor Stinner | 1ca5ba6 | 2013-12-03 01:49:43 +0100 | [diff] [blame] | 6 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 7 | .. _asyncio-transport: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 8 | |
| 9 | Transports |
| 10 | ========== |
| 11 | |
Guido van Rossum | 589872c | 2014-03-29 21:14:04 -0700 | [diff] [blame] | 12 | Transports are classes provided by :mod:`asyncio` in order to abstract |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 13 | various kinds of communication channels. You generally won't instantiate |
Martin Panter | d210a70 | 2016-08-20 08:03:06 +0000 | [diff] [blame] | 14 | a transport yourself; instead, you will call an :class:`AbstractEventLoop` method |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 15 | which will create the transport and try to initiate the underlying |
| 16 | communication channel, calling you back when it succeeds. |
| 17 | |
| 18 | Once the communication channel is established, a transport is always |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 19 | paired with a :ref:`protocol <asyncio-protocol>` instance. The protocol can |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 20 | then call the transport's methods for various purposes. |
| 21 | |
| 22 | :mod:`asyncio` currently implements transports for TCP, UDP, SSL, and |
| 23 | subprocess pipes. The methods available on a transport depend on |
| 24 | the transport's kind. |
| 25 | |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 26 | The transport classes are :ref:`not thread safe <asyncio-multithreading>`. |
| 27 | |
Yury Selivanov | 3432f2f | 2016-12-12 16:44:58 -0500 | [diff] [blame] | 28 | .. versionchanged:: 3.6 |
| 29 | The socket option ``TCP_NODELAY`` is now set by default. |
| 30 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 31 | |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 32 | BaseTransport |
| 33 | ------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 34 | |
| 35 | .. class:: BaseTransport |
| 36 | |
| 37 | Base class for transports. |
| 38 | |
Mariatta | 091b84f | 2017-02-27 05:44:15 -0800 | [diff] [blame] | 39 | .. method:: close() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 40 | |
| 41 | Close the transport. If the transport has a buffer for outgoing |
| 42 | data, buffered data will be flushed asynchronously. No more data |
| 43 | will be received. After all buffered data is flushed, the |
| 44 | protocol's :meth:`connection_lost` method will be called with |
| 45 | :const:`None` as its argument. |
| 46 | |
Mariatta | 091b84f | 2017-02-27 05:44:15 -0800 | [diff] [blame] | 47 | .. method:: is_closing() |
Yury Selivanov | 1744d53 | 2015-11-16 12:46:41 -0500 | [diff] [blame] | 48 | |
| 49 | Return ``True`` if the transport is closing or is closed. |
| 50 | |
Yury Selivanov | 344904c | 2015-11-16 12:47:15 -0500 | [diff] [blame] | 51 | .. versionadded:: 3.5.1 |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 52 | |
| 53 | .. method:: get_extra_info(name, default=None) |
| 54 | |
| 55 | Return optional transport information. *name* is a string representing |
| 56 | the piece of transport-specific information to get, *default* is the |
| 57 | value to return if the information doesn't exist. |
| 58 | |
| 59 | This method allows transport implementations to easily expose |
| 60 | channel-specific information. |
| 61 | |
| 62 | * socket: |
| 63 | |
| 64 | - ``'peername'``: the remote address to which the socket is connected, |
| 65 | result of :meth:`socket.socket.getpeername` (``None`` on error) |
| 66 | - ``'socket'``: :class:`socket.socket` instance |
| 67 | - ``'sockname'``: the socket's own address, |
| 68 | result of :meth:`socket.socket.getsockname` |
| 69 | |
| 70 | * SSL socket: |
| 71 | |
| 72 | - ``'compression'``: the compression algorithm being used as a string, |
| 73 | or ``None`` if the connection isn't compressed; result of |
| 74 | :meth:`ssl.SSLSocket.compression` |
| 75 | - ``'cipher'``: a three-value tuple containing the name of the cipher |
| 76 | being used, the version of the SSL protocol that defines its use, and |
| 77 | the number of secret bits being used; result of |
| 78 | :meth:`ssl.SSLSocket.cipher` |
| 79 | - ``'peercert'``: peer certificate; result of |
| 80 | :meth:`ssl.SSLSocket.getpeercert` |
| 81 | - ``'sslcontext'``: :class:`ssl.SSLContext` instance |
Victor Stinner | f7dc7fb | 2015-09-21 18:06:17 +0200 | [diff] [blame] | 82 | - ``'ssl_object'``: :class:`ssl.SSLObject` or :class:`ssl.SSLSocket` |
| 83 | instance |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 84 | |
| 85 | * pipe: |
| 86 | |
| 87 | - ``'pipe'``: pipe object |
| 88 | |
| 89 | * subprocess: |
| 90 | |
| 91 | - ``'subprocess'``: :class:`subprocess.Popen` instance |
| 92 | |
Berker Peksag | 2ebd6fe | 2016-11-07 23:36:14 +0300 | [diff] [blame] | 93 | .. method:: set_protocol(protocol) |
INADA Naoki | 1ea023e | 2016-11-04 16:33:47 +0900 | [diff] [blame] | 94 | |
| 95 | Set a new protocol. Switching protocol should only be done when both |
| 96 | protocols are documented to support the switch. |
| 97 | |
Berker Peksag | 2ebd6fe | 2016-11-07 23:36:14 +0300 | [diff] [blame] | 98 | .. versionadded:: 3.5.3 |
INADA Naoki | 1ea023e | 2016-11-04 16:33:47 +0900 | [diff] [blame] | 99 | |
Berker Peksag | 2ebd6fe | 2016-11-07 23:36:14 +0300 | [diff] [blame] | 100 | .. method:: get_protocol |
INADA Naoki | 1ea023e | 2016-11-04 16:33:47 +0900 | [diff] [blame] | 101 | |
| 102 | Return the current protocol. |
| 103 | |
Berker Peksag | 2ebd6fe | 2016-11-07 23:36:14 +0300 | [diff] [blame] | 104 | .. versionadded:: 3.5.3 |
INADA Naoki | 1ea023e | 2016-11-04 16:33:47 +0900 | [diff] [blame] | 105 | |
Victor Stinner | 04ce06b | 2015-09-21 18:27:52 +0200 | [diff] [blame] | 106 | .. versionchanged:: 3.5.1 |
Victor Stinner | f7dc7fb | 2015-09-21 18:06:17 +0200 | [diff] [blame] | 107 | ``'ssl_object'`` info was added to SSL sockets. |
| 108 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 109 | |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 110 | ReadTransport |
| 111 | ------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 112 | |
| 113 | .. class:: ReadTransport |
| 114 | |
| 115 | Interface for read-only transports. |
| 116 | |
| 117 | .. method:: pause_reading() |
| 118 | |
| 119 | Pause the receiving end of the transport. No data will be passed to |
Victor Stinner | 51f3129 | 2014-03-21 17:17:15 +0100 | [diff] [blame] | 120 | the protocol's :meth:`data_received` method until :meth:`resume_reading` |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 121 | is called. |
| 122 | |
| 123 | .. method:: resume_reading() |
| 124 | |
| 125 | Resume the receiving end. The protocol's :meth:`data_received` method |
| 126 | will be called once again if some data is available for reading. |
| 127 | |
| 128 | |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 129 | WriteTransport |
| 130 | -------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 131 | |
| 132 | .. class:: WriteTransport |
| 133 | |
| 134 | Interface for write-only transports. |
| 135 | |
| 136 | .. method:: abort() |
| 137 | |
| 138 | Close the transport immediately, without waiting for pending operations |
| 139 | to complete. Buffered data will be lost. No more data will be received. |
| 140 | The protocol's :meth:`connection_lost` method will eventually be |
| 141 | called with :const:`None` as its argument. |
| 142 | |
| 143 | .. method:: can_write_eof() |
| 144 | |
| 145 | Return :const:`True` if the transport supports :meth:`write_eof`, |
| 146 | :const:`False` if not. |
| 147 | |
| 148 | .. method:: get_write_buffer_size() |
| 149 | |
| 150 | Return the current size of the output buffer used by the transport. |
| 151 | |
Victor Stinner | 52bb949 | 2014-08-26 00:22:28 +0200 | [diff] [blame] | 152 | .. method:: get_write_buffer_limits() |
| 153 | |
| 154 | Get the *high*- and *low*-water limits for write flow control. Return a |
| 155 | tuple ``(low, high)`` where *low* and *high* are positive number of |
| 156 | bytes. |
| 157 | |
| 158 | Use :meth:`set_write_buffer_limits` to set the limits. |
| 159 | |
| 160 | .. versionadded:: 3.4.2 |
| 161 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 162 | .. method:: set_write_buffer_limits(high=None, low=None) |
| 163 | |
| 164 | Set the *high*- and *low*-water limits for write flow control. |
| 165 | |
Kojo Idrissa | 5200a7c | 2017-06-20 14:32:00 -0500 | [diff] [blame] | 166 | These two values (measured in number of |
| 167 | bytes) control when the protocol's |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 168 | :meth:`pause_writing` and :meth:`resume_writing` methods are called. |
| 169 | If specified, the low-water limit must be less than or equal to the |
| 170 | high-water limit. Neither *high* nor *low* can be negative. |
| 171 | |
Kojo Idrissa | 5200a7c | 2017-06-20 14:32:00 -0500 | [diff] [blame] | 172 | :meth:`pause_writing` is called when the buffer size becomes greater |
| 173 | than or equal to the *high* value. If writing has been paused, |
| 174 | :meth:`resume_writing` is called when the buffer size becomes less |
| 175 | than or equal to the *low* value. |
| 176 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 177 | The defaults are implementation-specific. If only the |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 178 | high-water limit is given, the low-water limit defaults to an |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 179 | implementation-specific value less than or equal to the |
| 180 | high-water limit. Setting *high* to zero forces *low* to zero as |
| 181 | well, and causes :meth:`pause_writing` to be called whenever the |
| 182 | buffer becomes non-empty. Setting *low* to zero causes |
| 183 | :meth:`resume_writing` to be called only once the buffer is empty. |
| 184 | Use of zero for either limit is generally sub-optimal as it |
| 185 | reduces opportunities for doing I/O and computation |
| 186 | concurrently. |
| 187 | |
Victor Stinner | 52bb949 | 2014-08-26 00:22:28 +0200 | [diff] [blame] | 188 | Use :meth:`get_write_buffer_limits` to get the limits. |
| 189 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 190 | .. method:: write(data) |
| 191 | |
| 192 | Write some *data* bytes to the transport. |
| 193 | |
| 194 | This method does not block; it buffers the data and arranges for it |
| 195 | to be sent out asynchronously. |
| 196 | |
| 197 | .. method:: writelines(list_of_data) |
| 198 | |
| 199 | Write a list (or any iterable) of data bytes to the transport. |
| 200 | This is functionally equivalent to calling :meth:`write` on each |
| 201 | element yielded by the iterable, but may be implemented more efficiently. |
| 202 | |
| 203 | .. method:: write_eof() |
| 204 | |
| 205 | Close the write end of the transport after flushing buffered data. |
| 206 | Data may still be received. |
| 207 | |
| 208 | This method can raise :exc:`NotImplementedError` if the transport |
| 209 | (e.g. SSL) doesn't support half-closes. |
| 210 | |
| 211 | |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 212 | DatagramTransport |
| 213 | ----------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 214 | |
| 215 | .. method:: DatagramTransport.sendto(data, addr=None) |
| 216 | |
| 217 | Send the *data* bytes to the remote peer given by *addr* (a |
| 218 | transport-dependent target address). If *addr* is :const:`None`, the |
| 219 | data is sent to the target address given on transport creation. |
| 220 | |
| 221 | This method does not block; it buffers the data and arranges for it |
| 222 | to be sent out asynchronously. |
| 223 | |
| 224 | .. method:: DatagramTransport.abort() |
| 225 | |
| 226 | Close the transport immediately, without waiting for pending operations |
| 227 | to complete. Buffered data will be lost. No more data will be received. |
| 228 | The protocol's :meth:`connection_lost` method will eventually be |
| 229 | called with :const:`None` as its argument. |
| 230 | |
| 231 | |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 232 | BaseSubprocessTransport |
| 233 | ----------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 234 | |
| 235 | .. class:: BaseSubprocessTransport |
| 236 | |
| 237 | .. method:: get_pid() |
| 238 | |
| 239 | Return the subprocess process id as an integer. |
| 240 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 241 | .. method:: get_pipe_transport(fd) |
| 242 | |
Brian Curtin | a1afeec | 2014-02-08 18:36:14 -0600 | [diff] [blame] | 243 | Return the transport for the communication pipe corresponding to the |
Victor Stinner | 4270a24 | 2014-10-13 23:56:43 +0200 | [diff] [blame] | 244 | integer file descriptor *fd*: |
| 245 | |
| 246 | * ``0``: readable streaming transport of the standard input (*stdin*), |
| 247 | or :const:`None` if the subprocess was not created with ``stdin=PIPE`` |
| 248 | * ``1``: writable streaming transport of the standard output (*stdout*), |
| 249 | or :const:`None` if the subprocess was not created with ``stdout=PIPE`` |
| 250 | * ``2``: writable streaming transport of the standard error (*stderr*), |
| 251 | or :const:`None` if the subprocess was not created with ``stderr=PIPE`` |
| 252 | * other *fd*: :const:`None` |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 253 | |
Victor Stinner | 933a8c8 | 2013-12-03 01:59:38 +0100 | [diff] [blame] | 254 | .. method:: get_returncode() |
| 255 | |
| 256 | Return the subprocess returncode as an integer or :const:`None` |
| 257 | if it hasn't returned, similarly to the |
| 258 | :attr:`subprocess.Popen.returncode` attribute. |
| 259 | |
Mariatta | 091b84f | 2017-02-27 05:44:15 -0800 | [diff] [blame] | 260 | .. method:: kill() |
Victor Stinner | 933a8c8 | 2013-12-03 01:59:38 +0100 | [diff] [blame] | 261 | |
Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 262 | Kill the subprocess, as in :meth:`subprocess.Popen.kill`. |
Victor Stinner | 933a8c8 | 2013-12-03 01:59:38 +0100 | [diff] [blame] | 263 | |
| 264 | On POSIX systems, the function sends SIGKILL to the subprocess. |
| 265 | On Windows, this method is an alias for :meth:`terminate`. |
| 266 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 267 | .. method:: send_signal(signal) |
| 268 | |
| 269 | Send the *signal* number to the subprocess, as in |
| 270 | :meth:`subprocess.Popen.send_signal`. |
| 271 | |
| 272 | .. method:: terminate() |
| 273 | |
| 274 | Ask the subprocess to stop, as in :meth:`subprocess.Popen.terminate`. |
| 275 | This method is an alias for the :meth:`close` method. |
| 276 | |
| 277 | On POSIX systems, this method sends SIGTERM to the subprocess. |
| 278 | On Windows, the Windows API function TerminateProcess() is called to |
| 279 | stop the subprocess. |
| 280 | |
Victor Stinner | 4270a24 | 2014-10-13 23:56:43 +0200 | [diff] [blame] | 281 | .. method:: close() |
| 282 | |
| 283 | Ask the subprocess to stop by calling the :meth:`terminate` method if the |
| 284 | subprocess hasn't returned yet, and close transports of all pipes |
| 285 | (*stdin*, *stdout* and *stderr*). |
| 286 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 287 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 288 | .. _asyncio-protocol: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 289 | |
| 290 | Protocols |
| 291 | ========= |
| 292 | |
| 293 | :mod:`asyncio` provides base classes that you can subclass to implement |
| 294 | your network protocols. Those classes are used in conjunction with |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 295 | :ref:`transports <asyncio-transport>` (see below): the protocol parses incoming |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 296 | data and asks for the writing of outgoing data, while the transport is |
| 297 | responsible for the actual I/O and buffering. |
| 298 | |
| 299 | When subclassing a protocol class, it is recommended you override certain |
| 300 | methods. Those methods are callbacks: they will be called by the transport |
| 301 | on certain events (for example when some data is received); you shouldn't |
| 302 | call them yourself, unless you are implementing a transport. |
| 303 | |
| 304 | .. note:: |
| 305 | All callbacks have default implementations, which are empty. Therefore, |
| 306 | you only need to implement the callbacks for the events in which you |
| 307 | are interested. |
| 308 | |
| 309 | |
| 310 | Protocol classes |
| 311 | ---------------- |
| 312 | |
| 313 | .. class:: Protocol |
| 314 | |
| 315 | The base class for implementing streaming protocols (for use with |
| 316 | e.g. TCP and SSL transports). |
| 317 | |
| 318 | .. class:: DatagramProtocol |
| 319 | |
| 320 | The base class for implementing datagram protocols (for use with |
| 321 | e.g. UDP transports). |
| 322 | |
| 323 | .. class:: SubprocessProtocol |
| 324 | |
| 325 | The base class for implementing protocols communicating with child |
| 326 | processes (through a set of unidirectional pipes). |
| 327 | |
| 328 | |
| 329 | Connection callbacks |
| 330 | -------------------- |
| 331 | |
Victor Stinner | 1538665 | 2014-06-10 09:19:26 +0200 | [diff] [blame] | 332 | These callbacks may be called on :class:`Protocol`, :class:`DatagramProtocol` |
| 333 | and :class:`SubprocessProtocol` instances: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 334 | |
| 335 | .. method:: BaseProtocol.connection_made(transport) |
| 336 | |
| 337 | Called when a connection is made. |
| 338 | |
| 339 | The *transport* argument is the transport representing the |
| 340 | connection. You are responsible for storing it somewhere |
| 341 | (e.g. as an attribute) if you need to. |
| 342 | |
| 343 | .. method:: BaseProtocol.connection_lost(exc) |
| 344 | |
| 345 | Called when the connection is lost or closed. |
| 346 | |
| 347 | The argument is either an exception object or :const:`None`. |
| 348 | The latter means a regular EOF is received, or the connection was |
| 349 | aborted or closed by this side of the connection. |
| 350 | |
Victor Stinner | 1538665 | 2014-06-10 09:19:26 +0200 | [diff] [blame] | 351 | :meth:`~BaseProtocol.connection_made` and :meth:`~BaseProtocol.connection_lost` |
| 352 | are called exactly once per successful connection. All other callbacks will be |
| 353 | called between those two methods, which allows for easier resource management |
| 354 | in your protocol implementation. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 355 | |
| 356 | The following callbacks may be called only on :class:`SubprocessProtocol` |
| 357 | instances: |
| 358 | |
| 359 | .. method:: SubprocessProtocol.pipe_data_received(fd, data) |
| 360 | |
| 361 | Called when the child process writes data into its stdout or stderr pipe. |
| 362 | *fd* is the integer file descriptor of the pipe. *data* is a non-empty |
| 363 | bytes object containing the data. |
| 364 | |
| 365 | .. method:: SubprocessProtocol.pipe_connection_lost(fd, exc) |
| 366 | |
| 367 | Called when one of the pipes communicating with the child process |
| 368 | is closed. *fd* is the integer file descriptor that was closed. |
| 369 | |
| 370 | .. method:: SubprocessProtocol.process_exited() |
| 371 | |
| 372 | Called when the child process has exited. |
| 373 | |
| 374 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 375 | Streaming protocols |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 376 | ------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 377 | |
| 378 | The following callbacks are called on :class:`Protocol` instances: |
| 379 | |
| 380 | .. method:: Protocol.data_received(data) |
| 381 | |
| 382 | Called when some data is received. *data* is a non-empty bytes object |
| 383 | containing the incoming data. |
| 384 | |
| 385 | .. note:: |
| 386 | Whether the data is buffered, chunked or reassembled depends on |
| 387 | the transport. In general, you shouldn't rely on specific semantics |
| 388 | and instead make your parsing generic and flexible enough. However, |
| 389 | data is always received in the correct order. |
| 390 | |
| 391 | .. method:: Protocol.eof_received() |
| 392 | |
Barry Warsaw | dd9a0a1 | 2017-04-07 14:18:14 -0400 | [diff] [blame] | 393 | Called when the other end signals it won't send any more data |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 394 | (for example by calling :meth:`write_eof`, if the other end also uses |
| 395 | asyncio). |
| 396 | |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 397 | This method may return a false value (including ``None``), in which case |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 398 | the transport will close itself. Conversely, if this method returns a |
| 399 | true value, closing the transport is up to the protocol. Since the |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 400 | default implementation returns ``None``, it implicitly closes the connection. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 401 | |
| 402 | .. note:: |
| 403 | Some transports such as SSL don't support half-closed connections, |
| 404 | in which case returning true from this method will not prevent closing |
| 405 | the connection. |
| 406 | |
| 407 | :meth:`data_received` can be called an arbitrary number of times during |
| 408 | a connection. However, :meth:`eof_received` is called at most once |
| 409 | and, if called, :meth:`data_received` won't be called after it. |
| 410 | |
Victor Stinner | 54a231d | 2015-01-29 13:33:15 +0100 | [diff] [blame] | 411 | State machine: |
| 412 | |
| 413 | start -> :meth:`~BaseProtocol.connection_made` |
| 414 | [-> :meth:`~Protocol.data_received` \*] |
| 415 | [-> :meth:`~Protocol.eof_received` ?] |
| 416 | -> :meth:`~BaseProtocol.connection_lost` -> end |
| 417 | |
| 418 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 419 | Datagram protocols |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 420 | ------------------ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 421 | |
| 422 | The following callbacks are called on :class:`DatagramProtocol` instances. |
| 423 | |
| 424 | .. method:: DatagramProtocol.datagram_received(data, addr) |
| 425 | |
| 426 | Called when a datagram is received. *data* is a bytes object containing |
| 427 | the incoming data. *addr* is the address of the peer sending the data; |
| 428 | the exact format depends on the transport. |
| 429 | |
| 430 | .. method:: DatagramProtocol.error_received(exc) |
| 431 | |
| 432 | Called when a previous send or receive operation raises an |
| 433 | :class:`OSError`. *exc* is the :class:`OSError` instance. |
| 434 | |
| 435 | This method is called in rare conditions, when the transport (e.g. UDP) |
| 436 | detects that a datagram couldn't be delivered to its recipient. |
| 437 | In many conditions though, undeliverable datagrams will be silently |
| 438 | dropped. |
| 439 | |
| 440 | |
| 441 | Flow control callbacks |
| 442 | ---------------------- |
| 443 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 444 | These callbacks may be called on :class:`Protocol`, |
| 445 | :class:`DatagramProtocol` and :class:`SubprocessProtocol` instances: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 446 | |
| 447 | .. method:: BaseProtocol.pause_writing() |
| 448 | |
| 449 | Called when the transport's buffer goes over the high-water mark. |
| 450 | |
| 451 | .. method:: BaseProtocol.resume_writing() |
| 452 | |
| 453 | Called when the transport's buffer drains below the low-water mark. |
| 454 | |
| 455 | |
| 456 | :meth:`pause_writing` and :meth:`resume_writing` calls are paired -- |
| 457 | :meth:`pause_writing` is called once when the buffer goes strictly over |
| 458 | the high-water mark (even if subsequent writes increases the buffer size |
| 459 | even more), and eventually :meth:`resume_writing` is called once when the |
| 460 | buffer size reaches the low-water mark. |
| 461 | |
| 462 | .. note:: |
| 463 | If the buffer size equals the high-water mark, |
| 464 | :meth:`pause_writing` is not called -- it must go strictly over. |
| 465 | Conversely, :meth:`resume_writing` is called when the buffer size is |
| 466 | equal or lower than the low-water mark. These end conditions |
| 467 | are important to ensure that things go as expected when either |
| 468 | mark is zero. |
| 469 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 470 | .. note:: |
| 471 | On BSD systems (OS X, FreeBSD, etc.) flow control is not supported |
| 472 | for :class:`DatagramProtocol`, because send failures caused by |
| 473 | writing too many packets cannot be detected easily. The socket |
| 474 | always appears 'ready' and excess packets are dropped; an |
| 475 | :class:`OSError` with errno set to :const:`errno.ENOBUFS` may or |
| 476 | may not be raised; if it is raised, it will be reported to |
| 477 | :meth:`DatagramProtocol.error_received` but otherwise ignored. |
| 478 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 479 | |
Victor Stinner | 4b4f9eb | 2014-01-24 17:33:20 +0100 | [diff] [blame] | 480 | Coroutines and protocols |
| 481 | ------------------------ |
| 482 | |
Yury Selivanov | 04356e1 | 2015-06-30 22:13:22 -0400 | [diff] [blame] | 483 | Coroutines can be scheduled in a protocol method using :func:`ensure_future`, |
| 484 | but there is no guarantee made about the execution order. Protocols are not |
| 485 | aware of coroutines created in protocol methods and so will not wait for them. |
Victor Stinner | 4b4f9eb | 2014-01-24 17:33:20 +0100 | [diff] [blame] | 486 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 487 | To have a reliable execution order, use :ref:`stream objects <asyncio-streams>` in a |
Victor Stinner | 4b4f9eb | 2014-01-24 17:33:20 +0100 | [diff] [blame] | 488 | coroutine with ``yield from``. For example, the :meth:`StreamWriter.drain` |
| 489 | coroutine can be used to wait until the write buffer is flushed. |
| 490 | |
| 491 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 492 | Protocol examples |
| 493 | ================= |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 494 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 495 | .. _asyncio-tcp-echo-client-protocol: |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 496 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 497 | TCP echo client protocol |
| 498 | ------------------------ |
| 499 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 500 | TCP echo client using the :meth:`AbstractEventLoop.create_connection` method, send |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 501 | data and wait until the connection is closed:: |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 502 | |
| 503 | import asyncio |
| 504 | |
Victor Stinner | cfbea3a | 2014-10-12 11:30:17 +0200 | [diff] [blame] | 505 | class EchoClientProtocol(asyncio.Protocol): |
Victor Stinner | 5366434 | 2014-10-12 11:35:09 +0200 | [diff] [blame] | 506 | def __init__(self, message, loop): |
| 507 | self.message = message |
| 508 | self.loop = loop |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 509 | |
| 510 | def connection_made(self, transport): |
Victor Stinner | 31d8322 | 2013-12-04 11:16:17 +0100 | [diff] [blame] | 511 | transport.write(self.message.encode()) |
Victor Stinner | 5366434 | 2014-10-12 11:35:09 +0200 | [diff] [blame] | 512 | print('Data sent: {!r}'.format(self.message)) |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 513 | |
| 514 | def data_received(self, data): |
Victor Stinner | 5366434 | 2014-10-12 11:35:09 +0200 | [diff] [blame] | 515 | print('Data received: {!r}'.format(data.decode())) |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 516 | |
| 517 | def connection_lost(self, exc): |
Victor Stinner | 5366434 | 2014-10-12 11:35:09 +0200 | [diff] [blame] | 518 | print('The server closed the connection') |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 519 | print('Stop the event loop') |
Victor Stinner | 5366434 | 2014-10-12 11:35:09 +0200 | [diff] [blame] | 520 | self.loop.stop() |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 521 | |
| 522 | loop = asyncio.get_event_loop() |
Victor Stinner | 5366434 | 2014-10-12 11:35:09 +0200 | [diff] [blame] | 523 | message = 'Hello World!' |
| 524 | coro = loop.create_connection(lambda: EchoClientProtocol(message, loop), |
| 525 | '127.0.0.1', 8888) |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 526 | loop.run_until_complete(coro) |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 527 | loop.run_forever() |
| 528 | loop.close() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 529 | |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 530 | The event loop is running twice. The |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 531 | :meth:`~AbstractEventLoop.run_until_complete` method is preferred in this short |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 532 | example to raise an exception if the server is not listening, instead of |
| 533 | having to write a short coroutine to handle the exception and stop the |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 534 | running loop. At :meth:`~AbstractEventLoop.run_until_complete` exit, the loop is |
Andrew Svetlov | 588517c | 2014-07-23 11:27:17 +0300 | [diff] [blame] | 535 | no longer running, so there is no need to stop the loop in case of an error. |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 536 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 537 | .. seealso:: |
Victor Stinner | c2721b4 | 2014-10-12 11:13:40 +0200 | [diff] [blame] | 538 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 539 | The :ref:`TCP echo client using streams <asyncio-tcp-echo-client-streams>` |
| 540 | example uses the :func:`asyncio.open_connection` function. |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 541 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 542 | |
| 543 | .. _asyncio-tcp-echo-server-protocol: |
| 544 | |
| 545 | TCP echo server protocol |
| 546 | ------------------------ |
| 547 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 548 | TCP echo server using the :meth:`AbstractEventLoop.create_server` method, send back |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 549 | received data and close the connection:: |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 550 | |
| 551 | import asyncio |
| 552 | |
Victor Stinner | cfbea3a | 2014-10-12 11:30:17 +0200 | [diff] [blame] | 553 | class EchoServerClientProtocol(asyncio.Protocol): |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 554 | def connection_made(self, transport): |
| 555 | peername = transport.get_extra_info('peername') |
Victor Stinner | c2721b4 | 2014-10-12 11:13:40 +0200 | [diff] [blame] | 556 | print('Connection from {}'.format(peername)) |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 557 | self.transport = transport |
| 558 | |
| 559 | def data_received(self, data): |
Victor Stinner | c2721b4 | 2014-10-12 11:13:40 +0200 | [diff] [blame] | 560 | message = data.decode() |
| 561 | print('Data received: {!r}'.format(message)) |
| 562 | |
| 563 | print('Send: {!r}'.format(message)) |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 564 | self.transport.write(data) |
| 565 | |
Victor Stinner | 5366434 | 2014-10-12 11:35:09 +0200 | [diff] [blame] | 566 | print('Close the client socket') |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 567 | self.transport.close() |
| 568 | |
| 569 | loop = asyncio.get_event_loop() |
Victor Stinner | cfbea3a | 2014-10-12 11:30:17 +0200 | [diff] [blame] | 570 | # Each client connection will create a new protocol instance |
| 571 | coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888) |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 572 | server = loop.run_until_complete(coro) |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 573 | |
Serhiy Storchaka | 0424eaf | 2015-09-12 17:45:25 +0300 | [diff] [blame] | 574 | # Serve requests until Ctrl+C is pressed |
Victor Stinner | c2721b4 | 2014-10-12 11:13:40 +0200 | [diff] [blame] | 575 | print('Serving on {}'.format(server.sockets[0].getsockname())) |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 576 | try: |
| 577 | loop.run_forever() |
| 578 | except KeyboardInterrupt: |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 579 | pass |
Victor Stinner | c2721b4 | 2014-10-12 11:13:40 +0200 | [diff] [blame] | 580 | |
| 581 | # Close the server |
| 582 | server.close() |
| 583 | loop.run_until_complete(server.wait_closed()) |
| 584 | loop.close() |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 585 | |
R David Murray | 530a69f | 2013-12-14 11:26:06 -0500 | [diff] [blame] | 586 | :meth:`Transport.close` can be called immediately after |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 587 | :meth:`WriteTransport.write` even if data are not sent yet on the socket: both |
| 588 | methods are asynchronous. ``yield from`` is not needed because these transport |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 589 | methods are not coroutines. |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 590 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 591 | .. seealso:: |
| 592 | |
| 593 | The :ref:`TCP echo server using streams <asyncio-tcp-echo-server-streams>` |
| 594 | example uses the :func:`asyncio.start_server` function. |
| 595 | |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 596 | |
| 597 | .. _asyncio-udp-echo-client-protocol: |
| 598 | |
| 599 | UDP echo client protocol |
| 600 | ------------------------ |
| 601 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 602 | UDP echo client using the :meth:`AbstractEventLoop.create_datagram_endpoint` |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 603 | method, send data and close the transport when we received the answer:: |
| 604 | |
| 605 | import asyncio |
| 606 | |
| 607 | class EchoClientProtocol: |
| 608 | def __init__(self, message, loop): |
| 609 | self.message = message |
| 610 | self.loop = loop |
| 611 | self.transport = None |
| 612 | |
| 613 | def connection_made(self, transport): |
| 614 | self.transport = transport |
| 615 | print('Send:', self.message) |
| 616 | self.transport.sendto(self.message.encode()) |
| 617 | |
| 618 | def datagram_received(self, data, addr): |
| 619 | print("Received:", data.decode()) |
| 620 | |
| 621 | print("Close the socket") |
| 622 | self.transport.close() |
| 623 | |
| 624 | def error_received(self, exc): |
| 625 | print('Error received:', exc) |
| 626 | |
| 627 | def connection_lost(self, exc): |
| 628 | print("Socket closed, stop the event loop") |
| 629 | loop = asyncio.get_event_loop() |
| 630 | loop.stop() |
| 631 | |
| 632 | loop = asyncio.get_event_loop() |
| 633 | message = "Hello World!" |
| 634 | connect = loop.create_datagram_endpoint( |
| 635 | lambda: EchoClientProtocol(message, loop), |
| 636 | remote_addr=('127.0.0.1', 9999)) |
| 637 | transport, protocol = loop.run_until_complete(connect) |
| 638 | loop.run_forever() |
| 639 | transport.close() |
| 640 | loop.close() |
| 641 | |
| 642 | |
| 643 | .. _asyncio-udp-echo-server-protocol: |
| 644 | |
| 645 | UDP echo server protocol |
| 646 | ------------------------ |
| 647 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 648 | UDP echo server using the :meth:`AbstractEventLoop.create_datagram_endpoint` |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 649 | method, send back received data:: |
| 650 | |
| 651 | import asyncio |
| 652 | |
Victor Stinner | cfbea3a | 2014-10-12 11:30:17 +0200 | [diff] [blame] | 653 | class EchoServerProtocol: |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 654 | def connection_made(self, transport): |
| 655 | self.transport = transport |
| 656 | |
| 657 | def datagram_received(self, data, addr): |
| 658 | message = data.decode() |
| 659 | print('Received %r from %s' % (message, addr)) |
| 660 | print('Send %r to %s' % (message, addr)) |
| 661 | self.transport.sendto(data, addr) |
| 662 | |
| 663 | loop = asyncio.get_event_loop() |
| 664 | print("Starting UDP server") |
Victor Stinner | cfbea3a | 2014-10-12 11:30:17 +0200 | [diff] [blame] | 665 | # One protocol instance will be created to serve all client requests |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 666 | listen = loop.create_datagram_endpoint( |
Victor Stinner | cfbea3a | 2014-10-12 11:30:17 +0200 | [diff] [blame] | 667 | EchoServerProtocol, local_addr=('127.0.0.1', 9999)) |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 668 | transport, protocol = loop.run_until_complete(listen) |
| 669 | |
| 670 | try: |
| 671 | loop.run_forever() |
| 672 | except KeyboardInterrupt: |
| 673 | pass |
| 674 | |
| 675 | transport.close() |
| 676 | loop.close() |
| 677 | |
| 678 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 679 | .. _asyncio-register-socket: |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 680 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 681 | Register an open socket to wait for data using a protocol |
| 682 | --------------------------------------------------------- |
| 683 | |
| 684 | Wait until a socket receives data using the |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 685 | :meth:`AbstractEventLoop.create_connection` method with a protocol, and then close |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 686 | the event loop :: |
| 687 | |
| 688 | import asyncio |
Victor Stinner | ccd8e34 | 2014-10-11 16:30:02 +0200 | [diff] [blame] | 689 | try: |
| 690 | from socket import socketpair |
| 691 | except ImportError: |
| 692 | from asyncio.windows_utils import socketpair |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 693 | |
| 694 | # Create a pair of connected sockets |
Victor Stinner | ccd8e34 | 2014-10-11 16:30:02 +0200 | [diff] [blame] | 695 | rsock, wsock = socketpair() |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 696 | loop = asyncio.get_event_loop() |
| 697 | |
| 698 | class MyProtocol(asyncio.Protocol): |
| 699 | transport = None |
| 700 | |
| 701 | def connection_made(self, transport): |
| 702 | self.transport = transport |
| 703 | |
| 704 | def data_received(self, data): |
| 705 | print("Received:", data.decode()) |
| 706 | |
| 707 | # We are done: close the transport (it will call connection_lost()) |
| 708 | self.transport.close() |
| 709 | |
| 710 | def connection_lost(self, exc): |
| 711 | # The socket has been closed, stop the event loop |
| 712 | loop.stop() |
| 713 | |
| 714 | # Register the socket to wait for data |
| 715 | connect_coro = loop.create_connection(MyProtocol, sock=rsock) |
| 716 | transport, protocol = loop.run_until_complete(connect_coro) |
| 717 | |
| 718 | # Simulate the reception of data from the network |
| 719 | loop.call_soon(wsock.send, 'abc'.encode()) |
| 720 | |
| 721 | # Run the event loop |
| 722 | loop.run_forever() |
| 723 | |
| 724 | # We are done, close sockets and the event loop |
| 725 | rsock.close() |
| 726 | wsock.close() |
| 727 | loop.close() |
| 728 | |
| 729 | .. seealso:: |
| 730 | |
| 731 | The :ref:`watch a file descriptor for read events |
| 732 | <asyncio-watch-read-event>` example uses the low-level |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 733 | :meth:`AbstractEventLoop.add_reader` method to register the file descriptor of a |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 734 | socket. |
| 735 | |
| 736 | The :ref:`register an open socket to wait for data using streams |
| 737 | <asyncio-register-socket-streams>` example uses high-level streams |
| 738 | created by the :func:`open_connection` function in a coroutine. |