blob: 3fbf51058673d9224e93b0dbbe8675e149e8e1c5 [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +01002
Yury Selivanovcba00532015-12-16 21:30:52 -05003++++++++++++++++++++++++++++++++++++++++++++++
4Transports and protocols (callback based API)
5++++++++++++++++++++++++++++++++++++++++++++++
Victor Stinner1ca5ba62013-12-03 01:49:43 +01006
Victor Stinner9592edb2014-02-02 15:03:02 +01007.. _asyncio-transport:
Victor Stinnerea3183f2013-12-03 01:08:00 +01008
9Transports
10==========
11
Guido van Rossum589872c2014-03-29 21:14:04 -070012Transports are classes provided by :mod:`asyncio` in order to abstract
Victor Stinnerea3183f2013-12-03 01:08:00 +010013various kinds of communication channels. You generally won't instantiate
Martin Panterd210a702016-08-20 08:03:06 +000014a transport yourself; instead, you will call an :class:`AbstractEventLoop` method
Victor Stinnerea3183f2013-12-03 01:08:00 +010015which will create the transport and try to initiate the underlying
16communication channel, calling you back when it succeeds.
17
18Once the communication channel is established, a transport is always
Victor Stinner9592edb2014-02-02 15:03:02 +010019paired with a :ref:`protocol <asyncio-protocol>` instance. The protocol can
Victor Stinnerea3183f2013-12-03 01:08:00 +010020then call the transport's methods for various purposes.
21
22:mod:`asyncio` currently implements transports for TCP, UDP, SSL, and
23subprocess pipes. The methods available on a transport depend on
24the transport's kind.
25
Victor Stinner83704962015-02-25 14:24:15 +010026The transport classes are :ref:`not thread safe <asyncio-multithreading>`.
27
Yury Selivanov3432f2f2016-12-12 16:44:58 -050028.. versionchanged:: 3.6
29 The socket option ``TCP_NODELAY`` is now set by default.
30
Victor Stinnerea3183f2013-12-03 01:08:00 +010031
Victor Stinner0c6f1ca2013-12-03 01:46:39 +010032BaseTransport
33-------------
Victor Stinnerea3183f2013-12-03 01:08:00 +010034
35.. class:: BaseTransport
36
37 Base class for transports.
38
39 .. method:: close(self)
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
Yury Selivanov1744d532015-11-16 12:46:41 -050047 .. method:: is_closing(self)
48
49 Return ``True`` if the transport is closing or is closed.
50
Yury Selivanov344904c2015-11-16 12:47:15 -050051 .. versionadded:: 3.5.1
Victor Stinnerea3183f2013-12-03 01:08:00 +010052
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 Stinnerf7dc7fb2015-09-21 18:06:17 +020082 - ``'ssl_object'``: :class:`ssl.SSLObject` or :class:`ssl.SSLSocket`
83 instance
Victor Stinnerea3183f2013-12-03 01:08:00 +010084
85 * pipe:
86
87 - ``'pipe'``: pipe object
88
89 * subprocess:
90
91 - ``'subprocess'``: :class:`subprocess.Popen` instance
92
Berker Peksag2ebd6fe2016-11-07 23:36:14 +030093 .. method:: set_protocol(protocol)
INADA Naoki1ea023e2016-11-04 16:33:47 +090094
95 Set a new protocol. Switching protocol should only be done when both
96 protocols are documented to support the switch.
97
Berker Peksag2ebd6fe2016-11-07 23:36:14 +030098 .. versionadded:: 3.5.3
INADA Naoki1ea023e2016-11-04 16:33:47 +090099
Berker Peksag2ebd6fe2016-11-07 23:36:14 +0300100 .. method:: get_protocol
INADA Naoki1ea023e2016-11-04 16:33:47 +0900101
102 Return the current protocol.
103
Berker Peksag2ebd6fe2016-11-07 23:36:14 +0300104 .. versionadded:: 3.5.3
INADA Naoki1ea023e2016-11-04 16:33:47 +0900105
Victor Stinner04ce06b2015-09-21 18:27:52 +0200106 .. versionchanged:: 3.5.1
Victor Stinnerf7dc7fb2015-09-21 18:06:17 +0200107 ``'ssl_object'`` info was added to SSL sockets.
108
Victor Stinnerea3183f2013-12-03 01:08:00 +0100109
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100110ReadTransport
111-------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100112
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 Stinner51f31292014-03-21 17:17:15 +0100120 the protocol's :meth:`data_received` method until :meth:`resume_reading`
Victor Stinnerea3183f2013-12-03 01:08:00 +0100121 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 Stinner0c6f1ca2013-12-03 01:46:39 +0100129WriteTransport
130--------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100131
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 Stinner52bb9492014-08-26 00:22:28 +0200152 .. 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 Stinnerea3183f2013-12-03 01:08:00 +0100162 .. method:: set_write_buffer_limits(high=None, low=None)
163
164 Set the *high*- and *low*-water limits for write flow control.
165
166 These two values control when call the protocol's
167 :meth:`pause_writing` and :meth:`resume_writing` methods are called.
168 If specified, the low-water limit must be less than or equal to the
169 high-water limit. Neither *high* nor *low* can be negative.
170
171 The defaults are implementation-specific. If only the
Serhiy Storchakad65c9492015-11-02 14:10:23 +0200172 high-water limit is given, the low-water limit defaults to an
Victor Stinnerea3183f2013-12-03 01:08:00 +0100173 implementation-specific value less than or equal to the
174 high-water limit. Setting *high* to zero forces *low* to zero as
175 well, and causes :meth:`pause_writing` to be called whenever the
176 buffer becomes non-empty. Setting *low* to zero causes
177 :meth:`resume_writing` to be called only once the buffer is empty.
178 Use of zero for either limit is generally sub-optimal as it
179 reduces opportunities for doing I/O and computation
180 concurrently.
181
Victor Stinner52bb9492014-08-26 00:22:28 +0200182 Use :meth:`get_write_buffer_limits` to get the limits.
183
Victor Stinnerea3183f2013-12-03 01:08:00 +0100184 .. method:: write(data)
185
186 Write some *data* bytes to the transport.
187
188 This method does not block; it buffers the data and arranges for it
189 to be sent out asynchronously.
190
191 .. method:: writelines(list_of_data)
192
193 Write a list (or any iterable) of data bytes to the transport.
194 This is functionally equivalent to calling :meth:`write` on each
195 element yielded by the iterable, but may be implemented more efficiently.
196
197 .. method:: write_eof()
198
199 Close the write end of the transport after flushing buffered data.
200 Data may still be received.
201
202 This method can raise :exc:`NotImplementedError` if the transport
203 (e.g. SSL) doesn't support half-closes.
204
205
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100206DatagramTransport
207-----------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100208
209.. method:: DatagramTransport.sendto(data, addr=None)
210
211 Send the *data* bytes to the remote peer given by *addr* (a
212 transport-dependent target address). If *addr* is :const:`None`, the
213 data is sent to the target address given on transport creation.
214
215 This method does not block; it buffers the data and arranges for it
216 to be sent out asynchronously.
217
218.. method:: DatagramTransport.abort()
219
220 Close the transport immediately, without waiting for pending operations
221 to complete. Buffered data will be lost. No more data will be received.
222 The protocol's :meth:`connection_lost` method will eventually be
223 called with :const:`None` as its argument.
224
225
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100226BaseSubprocessTransport
227-----------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100228
229.. class:: BaseSubprocessTransport
230
231 .. method:: get_pid()
232
233 Return the subprocess process id as an integer.
234
Victor Stinnerea3183f2013-12-03 01:08:00 +0100235 .. method:: get_pipe_transport(fd)
236
Brian Curtina1afeec2014-02-08 18:36:14 -0600237 Return the transport for the communication pipe corresponding to the
Victor Stinner4270a242014-10-13 23:56:43 +0200238 integer file descriptor *fd*:
239
240 * ``0``: readable streaming transport of the standard input (*stdin*),
241 or :const:`None` if the subprocess was not created with ``stdin=PIPE``
242 * ``1``: writable streaming transport of the standard output (*stdout*),
243 or :const:`None` if the subprocess was not created with ``stdout=PIPE``
244 * ``2``: writable streaming transport of the standard error (*stderr*),
245 or :const:`None` if the subprocess was not created with ``stderr=PIPE``
246 * other *fd*: :const:`None`
Victor Stinnerea3183f2013-12-03 01:08:00 +0100247
Victor Stinner933a8c82013-12-03 01:59:38 +0100248 .. method:: get_returncode()
249
250 Return the subprocess returncode as an integer or :const:`None`
251 if it hasn't returned, similarly to the
252 :attr:`subprocess.Popen.returncode` attribute.
253
254 .. method:: kill(self)
255
Martin Panterd21e0b52015-10-10 10:36:22 +0000256 Kill the subprocess, as in :meth:`subprocess.Popen.kill`.
Victor Stinner933a8c82013-12-03 01:59:38 +0100257
258 On POSIX systems, the function sends SIGKILL to the subprocess.
259 On Windows, this method is an alias for :meth:`terminate`.
260
Victor Stinnerea3183f2013-12-03 01:08:00 +0100261 .. method:: send_signal(signal)
262
263 Send the *signal* number to the subprocess, as in
264 :meth:`subprocess.Popen.send_signal`.
265
266 .. method:: terminate()
267
268 Ask the subprocess to stop, as in :meth:`subprocess.Popen.terminate`.
269 This method is an alias for the :meth:`close` method.
270
271 On POSIX systems, this method sends SIGTERM to the subprocess.
272 On Windows, the Windows API function TerminateProcess() is called to
273 stop the subprocess.
274
Victor Stinner4270a242014-10-13 23:56:43 +0200275 .. method:: close()
276
277 Ask the subprocess to stop by calling the :meth:`terminate` method if the
278 subprocess hasn't returned yet, and close transports of all pipes
279 (*stdin*, *stdout* and *stderr*).
280
Victor Stinnerea3183f2013-12-03 01:08:00 +0100281
Victor Stinner9592edb2014-02-02 15:03:02 +0100282.. _asyncio-protocol:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100283
284Protocols
285=========
286
287:mod:`asyncio` provides base classes that you can subclass to implement
288your network protocols. Those classes are used in conjunction with
Victor Stinner9592edb2014-02-02 15:03:02 +0100289:ref:`transports <asyncio-transport>` (see below): the protocol parses incoming
Victor Stinnerea3183f2013-12-03 01:08:00 +0100290data and asks for the writing of outgoing data, while the transport is
291responsible for the actual I/O and buffering.
292
293When subclassing a protocol class, it is recommended you override certain
294methods. Those methods are callbacks: they will be called by the transport
295on certain events (for example when some data is received); you shouldn't
296call them yourself, unless you are implementing a transport.
297
298.. note::
299 All callbacks have default implementations, which are empty. Therefore,
300 you only need to implement the callbacks for the events in which you
301 are interested.
302
303
304Protocol classes
305----------------
306
307.. class:: Protocol
308
309 The base class for implementing streaming protocols (for use with
310 e.g. TCP and SSL transports).
311
312.. class:: DatagramProtocol
313
314 The base class for implementing datagram protocols (for use with
315 e.g. UDP transports).
316
317.. class:: SubprocessProtocol
318
319 The base class for implementing protocols communicating with child
320 processes (through a set of unidirectional pipes).
321
322
323Connection callbacks
324--------------------
325
Victor Stinner15386652014-06-10 09:19:26 +0200326These callbacks may be called on :class:`Protocol`, :class:`DatagramProtocol`
327and :class:`SubprocessProtocol` instances:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100328
329.. method:: BaseProtocol.connection_made(transport)
330
331 Called when a connection is made.
332
333 The *transport* argument is the transport representing the
334 connection. You are responsible for storing it somewhere
335 (e.g. as an attribute) if you need to.
336
337.. method:: BaseProtocol.connection_lost(exc)
338
339 Called when the connection is lost or closed.
340
341 The argument is either an exception object or :const:`None`.
342 The latter means a regular EOF is received, or the connection was
343 aborted or closed by this side of the connection.
344
Victor Stinner15386652014-06-10 09:19:26 +0200345:meth:`~BaseProtocol.connection_made` and :meth:`~BaseProtocol.connection_lost`
346are called exactly once per successful connection. All other callbacks will be
347called between those two methods, which allows for easier resource management
348in your protocol implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100349
350The following callbacks may be called only on :class:`SubprocessProtocol`
351instances:
352
353.. method:: SubprocessProtocol.pipe_data_received(fd, data)
354
355 Called when the child process writes data into its stdout or stderr pipe.
356 *fd* is the integer file descriptor of the pipe. *data* is a non-empty
357 bytes object containing the data.
358
359.. method:: SubprocessProtocol.pipe_connection_lost(fd, exc)
360
361 Called when one of the pipes communicating with the child process
362 is closed. *fd* is the integer file descriptor that was closed.
363
364.. method:: SubprocessProtocol.process_exited()
365
366 Called when the child process has exited.
367
368
Victor Stinnerea3183f2013-12-03 01:08:00 +0100369Streaming protocols
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100370-------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100371
372The following callbacks are called on :class:`Protocol` instances:
373
374.. method:: Protocol.data_received(data)
375
376 Called when some data is received. *data* is a non-empty bytes object
377 containing the incoming data.
378
379 .. note::
380 Whether the data is buffered, chunked or reassembled depends on
381 the transport. In general, you shouldn't rely on specific semantics
382 and instead make your parsing generic and flexible enough. However,
383 data is always received in the correct order.
384
385.. method:: Protocol.eof_received()
386
387 Calls when the other end signals it won't send any more data
388 (for example by calling :meth:`write_eof`, if the other end also uses
389 asyncio).
390
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300391 This method may return a false value (including ``None``), in which case
Victor Stinnerea3183f2013-12-03 01:08:00 +0100392 the transport will close itself. Conversely, if this method returns a
393 true value, closing the transport is up to the protocol. Since the
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300394 default implementation returns ``None``, it implicitly closes the connection.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100395
396 .. note::
397 Some transports such as SSL don't support half-closed connections,
398 in which case returning true from this method will not prevent closing
399 the connection.
400
401:meth:`data_received` can be called an arbitrary number of times during
402a connection. However, :meth:`eof_received` is called at most once
403and, if called, :meth:`data_received` won't be called after it.
404
Victor Stinner54a231d2015-01-29 13:33:15 +0100405State machine:
406
407 start -> :meth:`~BaseProtocol.connection_made`
408 [-> :meth:`~Protocol.data_received` \*]
409 [-> :meth:`~Protocol.eof_received` ?]
410 -> :meth:`~BaseProtocol.connection_lost` -> end
411
412
Victor Stinnerea3183f2013-12-03 01:08:00 +0100413Datagram protocols
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100414------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100415
416The following callbacks are called on :class:`DatagramProtocol` instances.
417
418.. method:: DatagramProtocol.datagram_received(data, addr)
419
420 Called when a datagram is received. *data* is a bytes object containing
421 the incoming data. *addr* is the address of the peer sending the data;
422 the exact format depends on the transport.
423
424.. method:: DatagramProtocol.error_received(exc)
425
426 Called when a previous send or receive operation raises an
427 :class:`OSError`. *exc* is the :class:`OSError` instance.
428
429 This method is called in rare conditions, when the transport (e.g. UDP)
430 detects that a datagram couldn't be delivered to its recipient.
431 In many conditions though, undeliverable datagrams will be silently
432 dropped.
433
434
435Flow control callbacks
436----------------------
437
Larry Hastings3732ed22014-03-15 21:13:56 -0700438These callbacks may be called on :class:`Protocol`,
439:class:`DatagramProtocol` and :class:`SubprocessProtocol` instances:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100440
441.. method:: BaseProtocol.pause_writing()
442
443 Called when the transport's buffer goes over the high-water mark.
444
445.. method:: BaseProtocol.resume_writing()
446
447 Called when the transport's buffer drains below the low-water mark.
448
449
450:meth:`pause_writing` and :meth:`resume_writing` calls are paired --
451:meth:`pause_writing` is called once when the buffer goes strictly over
452the high-water mark (even if subsequent writes increases the buffer size
453even more), and eventually :meth:`resume_writing` is called once when the
454buffer size reaches the low-water mark.
455
456.. note::
457 If the buffer size equals the high-water mark,
458 :meth:`pause_writing` is not called -- it must go strictly over.
459 Conversely, :meth:`resume_writing` is called when the buffer size is
460 equal or lower than the low-water mark. These end conditions
461 are important to ensure that things go as expected when either
462 mark is zero.
463
Larry Hastings3732ed22014-03-15 21:13:56 -0700464.. note::
465 On BSD systems (OS X, FreeBSD, etc.) flow control is not supported
466 for :class:`DatagramProtocol`, because send failures caused by
467 writing too many packets cannot be detected easily. The socket
468 always appears 'ready' and excess packets are dropped; an
469 :class:`OSError` with errno set to :const:`errno.ENOBUFS` may or
470 may not be raised; if it is raised, it will be reported to
471 :meth:`DatagramProtocol.error_received` but otherwise ignored.
472
Victor Stinnerea3183f2013-12-03 01:08:00 +0100473
Victor Stinner4b4f9eb2014-01-24 17:33:20 +0100474Coroutines and protocols
475------------------------
476
Yury Selivanov04356e12015-06-30 22:13:22 -0400477Coroutines can be scheduled in a protocol method using :func:`ensure_future`,
478but there is no guarantee made about the execution order. Protocols are not
479aware of coroutines created in protocol methods and so will not wait for them.
Victor Stinner4b4f9eb2014-01-24 17:33:20 +0100480
Victor Stinner9592edb2014-02-02 15:03:02 +0100481To have a reliable execution order, use :ref:`stream objects <asyncio-streams>` in a
Victor Stinner4b4f9eb2014-01-24 17:33:20 +0100482coroutine with ``yield from``. For example, the :meth:`StreamWriter.drain`
483coroutine can be used to wait until the write buffer is flushed.
484
485
Victor Stinner04e6df32014-10-11 16:16:27 +0200486Protocol examples
487=================
Victor Stinnerea3183f2013-12-03 01:08:00 +0100488
Victor Stinnered051592014-10-12 20:18:16 +0200489.. _asyncio-tcp-echo-client-protocol:
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100490
Victor Stinnered051592014-10-12 20:18:16 +0200491TCP echo client protocol
492------------------------
493
Guido van Rossumf68afd82016-08-08 09:41:21 -0700494TCP echo client using the :meth:`AbstractEventLoop.create_connection` method, send
Victor Stinnered051592014-10-12 20:18:16 +0200495data and wait until the connection is closed::
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100496
497 import asyncio
498
Victor Stinnercfbea3a2014-10-12 11:30:17 +0200499 class EchoClientProtocol(asyncio.Protocol):
Victor Stinner53664342014-10-12 11:35:09 +0200500 def __init__(self, message, loop):
501 self.message = message
502 self.loop = loop
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100503
504 def connection_made(self, transport):
Victor Stinner31d83222013-12-04 11:16:17 +0100505 transport.write(self.message.encode())
Victor Stinner53664342014-10-12 11:35:09 +0200506 print('Data sent: {!r}'.format(self.message))
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100507
508 def data_received(self, data):
Victor Stinner53664342014-10-12 11:35:09 +0200509 print('Data received: {!r}'.format(data.decode()))
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100510
511 def connection_lost(self, exc):
Victor Stinner53664342014-10-12 11:35:09 +0200512 print('The server closed the connection')
Guido van Rossum41f69f42015-11-19 13:28:47 -0800513 print('Stop the event loop')
Victor Stinner53664342014-10-12 11:35:09 +0200514 self.loop.stop()
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100515
516 loop = asyncio.get_event_loop()
Victor Stinner53664342014-10-12 11:35:09 +0200517 message = 'Hello World!'
518 coro = loop.create_connection(lambda: EchoClientProtocol(message, loop),
519 '127.0.0.1', 8888)
Victor Stinnera881a7f2013-12-09 13:19:23 +0100520 loop.run_until_complete(coro)
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100521 loop.run_forever()
522 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100523
Victor Stinnera881a7f2013-12-09 13:19:23 +0100524The event loop is running twice. The
Guido van Rossumf68afd82016-08-08 09:41:21 -0700525:meth:`~AbstractEventLoop.run_until_complete` method is preferred in this short
Victor Stinnera881a7f2013-12-09 13:19:23 +0100526example to raise an exception if the server is not listening, instead of
527having to write a short coroutine to handle the exception and stop the
Guido van Rossumf68afd82016-08-08 09:41:21 -0700528running loop. At :meth:`~AbstractEventLoop.run_until_complete` exit, the loop is
Andrew Svetlov588517c2014-07-23 11:27:17 +0300529no longer running, so there is no need to stop the loop in case of an error.
Victor Stinnera881a7f2013-12-09 13:19:23 +0100530
Victor Stinnered051592014-10-12 20:18:16 +0200531.. seealso::
Victor Stinnerc2721b42014-10-12 11:13:40 +0200532
Victor Stinnered051592014-10-12 20:18:16 +0200533 The :ref:`TCP echo client using streams <asyncio-tcp-echo-client-streams>`
534 example uses the :func:`asyncio.open_connection` function.
Victor Stinnera881a7f2013-12-09 13:19:23 +0100535
Victor Stinnered051592014-10-12 20:18:16 +0200536
537.. _asyncio-tcp-echo-server-protocol:
538
539TCP echo server protocol
540------------------------
541
Guido van Rossumf68afd82016-08-08 09:41:21 -0700542TCP echo server using the :meth:`AbstractEventLoop.create_server` method, send back
Victor Stinnered051592014-10-12 20:18:16 +0200543received data and close the connection::
Victor Stinnera881a7f2013-12-09 13:19:23 +0100544
545 import asyncio
546
Victor Stinnercfbea3a2014-10-12 11:30:17 +0200547 class EchoServerClientProtocol(asyncio.Protocol):
Victor Stinnera881a7f2013-12-09 13:19:23 +0100548 def connection_made(self, transport):
549 peername = transport.get_extra_info('peername')
Victor Stinnerc2721b42014-10-12 11:13:40 +0200550 print('Connection from {}'.format(peername))
Victor Stinnera881a7f2013-12-09 13:19:23 +0100551 self.transport = transport
552
553 def data_received(self, data):
Victor Stinnerc2721b42014-10-12 11:13:40 +0200554 message = data.decode()
555 print('Data received: {!r}'.format(message))
556
557 print('Send: {!r}'.format(message))
Victor Stinnera881a7f2013-12-09 13:19:23 +0100558 self.transport.write(data)
559
Victor Stinner53664342014-10-12 11:35:09 +0200560 print('Close the client socket')
Victor Stinnera881a7f2013-12-09 13:19:23 +0100561 self.transport.close()
562
563 loop = asyncio.get_event_loop()
Victor Stinnercfbea3a2014-10-12 11:30:17 +0200564 # Each client connection will create a new protocol instance
565 coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
Victor Stinnera881a7f2013-12-09 13:19:23 +0100566 server = loop.run_until_complete(coro)
Victor Stinnera881a7f2013-12-09 13:19:23 +0100567
Serhiy Storchaka0424eaf2015-09-12 17:45:25 +0300568 # Serve requests until Ctrl+C is pressed
Victor Stinnerc2721b42014-10-12 11:13:40 +0200569 print('Serving on {}'.format(server.sockets[0].getsockname()))
Victor Stinnera881a7f2013-12-09 13:19:23 +0100570 try:
571 loop.run_forever()
572 except KeyboardInterrupt:
Victor Stinnered051592014-10-12 20:18:16 +0200573 pass
Victor Stinnerc2721b42014-10-12 11:13:40 +0200574
575 # Close the server
576 server.close()
577 loop.run_until_complete(server.wait_closed())
578 loop.close()
Victor Stinnera881a7f2013-12-09 13:19:23 +0100579
R David Murray530a69f2013-12-14 11:26:06 -0500580:meth:`Transport.close` can be called immediately after
Victor Stinnera881a7f2013-12-09 13:19:23 +0100581:meth:`WriteTransport.write` even if data are not sent yet on the socket: both
582methods are asynchronous. ``yield from`` is not needed because these transport
Larry Hastings3732ed22014-03-15 21:13:56 -0700583methods are not coroutines.
Victor Stinnera881a7f2013-12-09 13:19:23 +0100584
Victor Stinnered051592014-10-12 20:18:16 +0200585.. seealso::
586
587 The :ref:`TCP echo server using streams <asyncio-tcp-echo-server-streams>`
588 example uses the :func:`asyncio.start_server` function.
589
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200590
591.. _asyncio-udp-echo-client-protocol:
592
593UDP echo client protocol
594------------------------
595
Guido van Rossumf68afd82016-08-08 09:41:21 -0700596UDP echo client using the :meth:`AbstractEventLoop.create_datagram_endpoint`
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200597method, send data and close the transport when we received the answer::
598
599 import asyncio
600
601 class EchoClientProtocol:
602 def __init__(self, message, loop):
603 self.message = message
604 self.loop = loop
605 self.transport = None
606
607 def connection_made(self, transport):
608 self.transport = transport
609 print('Send:', self.message)
610 self.transport.sendto(self.message.encode())
611
612 def datagram_received(self, data, addr):
613 print("Received:", data.decode())
614
615 print("Close the socket")
616 self.transport.close()
617
618 def error_received(self, exc):
619 print('Error received:', exc)
620
621 def connection_lost(self, exc):
622 print("Socket closed, stop the event loop")
623 loop = asyncio.get_event_loop()
624 loop.stop()
625
626 loop = asyncio.get_event_loop()
627 message = "Hello World!"
628 connect = loop.create_datagram_endpoint(
629 lambda: EchoClientProtocol(message, loop),
630 remote_addr=('127.0.0.1', 9999))
631 transport, protocol = loop.run_until_complete(connect)
632 loop.run_forever()
633 transport.close()
634 loop.close()
635
636
637.. _asyncio-udp-echo-server-protocol:
638
639UDP echo server protocol
640------------------------
641
Guido van Rossumf68afd82016-08-08 09:41:21 -0700642UDP echo server using the :meth:`AbstractEventLoop.create_datagram_endpoint`
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200643method, send back received data::
644
645 import asyncio
646
Victor Stinnercfbea3a2014-10-12 11:30:17 +0200647 class EchoServerProtocol:
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200648 def connection_made(self, transport):
649 self.transport = transport
650
651 def datagram_received(self, data, addr):
652 message = data.decode()
653 print('Received %r from %s' % (message, addr))
654 print('Send %r to %s' % (message, addr))
655 self.transport.sendto(data, addr)
656
657 loop = asyncio.get_event_loop()
658 print("Starting UDP server")
Victor Stinnercfbea3a2014-10-12 11:30:17 +0200659 # One protocol instance will be created to serve all client requests
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200660 listen = loop.create_datagram_endpoint(
Victor Stinnercfbea3a2014-10-12 11:30:17 +0200661 EchoServerProtocol, local_addr=('127.0.0.1', 9999))
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200662 transport, protocol = loop.run_until_complete(listen)
663
664 try:
665 loop.run_forever()
666 except KeyboardInterrupt:
667 pass
668
669 transport.close()
670 loop.close()
671
672
Victor Stinner04e6df32014-10-11 16:16:27 +0200673.. _asyncio-register-socket:
Victor Stinnera881a7f2013-12-09 13:19:23 +0100674
Victor Stinner04e6df32014-10-11 16:16:27 +0200675Register an open socket to wait for data using a protocol
676---------------------------------------------------------
677
678Wait until a socket receives data using the
Guido van Rossumf68afd82016-08-08 09:41:21 -0700679:meth:`AbstractEventLoop.create_connection` method with a protocol, and then close
Victor Stinner04e6df32014-10-11 16:16:27 +0200680the event loop ::
681
682 import asyncio
Victor Stinnerccd8e342014-10-11 16:30:02 +0200683 try:
684 from socket import socketpair
685 except ImportError:
686 from asyncio.windows_utils import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +0200687
688 # Create a pair of connected sockets
Victor Stinnerccd8e342014-10-11 16:30:02 +0200689 rsock, wsock = socketpair()
Victor Stinner04e6df32014-10-11 16:16:27 +0200690 loop = asyncio.get_event_loop()
691
692 class MyProtocol(asyncio.Protocol):
693 transport = None
694
695 def connection_made(self, transport):
696 self.transport = transport
697
698 def data_received(self, data):
699 print("Received:", data.decode())
700
701 # We are done: close the transport (it will call connection_lost())
702 self.transport.close()
703
704 def connection_lost(self, exc):
705 # The socket has been closed, stop the event loop
706 loop.stop()
707
708 # Register the socket to wait for data
709 connect_coro = loop.create_connection(MyProtocol, sock=rsock)
710 transport, protocol = loop.run_until_complete(connect_coro)
711
712 # Simulate the reception of data from the network
713 loop.call_soon(wsock.send, 'abc'.encode())
714
715 # Run the event loop
716 loop.run_forever()
717
718 # We are done, close sockets and the event loop
719 rsock.close()
720 wsock.close()
721 loop.close()
722
723.. seealso::
724
725 The :ref:`watch a file descriptor for read events
726 <asyncio-watch-read-event>` example uses the low-level
Guido van Rossumf68afd82016-08-08 09:41:21 -0700727 :meth:`AbstractEventLoop.add_reader` method to register the file descriptor of a
Victor Stinner04e6df32014-10-11 16:16:27 +0200728 socket.
729
730 The :ref:`register an open socket to wait for data using streams
731 <asyncio-register-socket-streams>` example uses high-level streams
732 created by the :func:`open_connection` function in a coroutine.