the transport's kind.
-BaseTransport: Methods common to all transports
------------------------------------------------
+BaseTransport
+-------------
.. class:: BaseTransport
- ``'subprocess'``: :class:`subprocess.Popen` instance
-ReadTransport: Methods of readable streaming transports
--------------------------------------------------------
+ReadTransport
+-------------
.. class:: ReadTransport
will be called once again if some data is available for reading.
-WriteTransport: Methods of writable streaming transports
---------------------------------------------------------
+WriteTransport
+--------------
.. class:: WriteTransport
(e.g. SSL) doesn't support half-closes.
-DatagramTransport: Methods of datagram transports
--------------------------------------------------
+DatagramTransport
+-----------------
.. method:: DatagramTransport.sendto(data, addr=None)
called with :const:`None` as its argument.
-Methods of subprocess transports
---------------------------------
+BaseSubprocessTransport
+-----------------------
.. class:: BaseSubprocessTransport
On Windows, this method is an alias for :meth:`terminate`.
-Stream reader
--------------
+StreamWriter
+------------
.. class:: StreamWriter(transport, protocol, reader, loop)
see :meth:`WriteTransport.write_eof`.
-Stream writer
--------------
+StreamReader
+------------
.. class:: StreamReader(limit=_DEFAULT_LIMIT, loop=None)
Called when the child process has exited.
-Data reception callbacks
-------------------------
-
Streaming protocols
-^^^^^^^^^^^^^^^^^^^
+-------------------
The following callbacks are called on :class:`Protocol` instances:
and, if called, :meth:`data_received` won't be called after it.
Datagram protocols
-^^^^^^^^^^^^^^^^^^
+------------------
The following callbacks are called on :class:`DatagramProtocol` instances.
This function returns a :ref:`coroutine <coroutine>`.
-Example: Echo server
---------------------
-A :class:`Protocol` implementing an echo server::
+Protocol example: TCP echo client and server
+============================================
+
+Echo server
+-----------
+
+TCP echo server example::
+
+ import asyncio
+
+ class EchoServer(asyncio.Protocol):
+ def timeout(self):
+ print('connection timeout, closing.')
+ self.transport.close()
+
+ def connection_made(self, transport):
+ print('connection made')
+ self.transport = transport
+
+ # close the client connection after 2 seconds
+ asyncio.get_event_loop().call_later(2.0, self.timeout)
+
+ def data_received(self, data):
+ print('data received:', data.decode())
+ self.transport.write(data)
+
+ def connection_lost(self, exc):
+ print('connection lost')
+
+
+ loop = asyncio.get_event_loop()
+ f = loop.create_server(EchoServer, '127.0.0.1', 8888)
+ s = loop.run_until_complete(f)
+ print('serving on', s.sockets[0].getsockname())
+ loop.run_forever()
- class EchoServer(asyncio.Protocol):
- TIMEOUT = 5.0
+Echo client
+-----------
- def timeout(self):
- print('connection timeout, closing.')
- self.transport.close()
+TCP echo client example::
- def connection_made(self, transport):
- print('connection made')
- self.transport = transport
+ import asyncio
- # start 5 seconds timeout timer
- self.h_timeout = asyncio.get_event_loop().call_later(
- self.TIMEOUT, self.timeout)
+ class EchoClient(asyncio.Protocol):
+ message = 'This is the message. It will be echoed.'
- def data_received(self, data):
- print('data received: ', data.decode())
- self.transport.write(b'Re: ' + data)
+ def connection_made(self, transport):
+ self.transport = transport
+ self.transport.write(self.message.encode())
+ print('data sent:', self.message)
- # restart timeout timer
- self.h_timeout.cancel()
- self.h_timeout = asyncio.get_event_loop().call_later(
- self.TIMEOUT, self.timeout)
+ def data_received(self, data):
+ print('data received:', data.decode())
- def eof_received(self):
- pass
+ def connection_lost(self, exc):
+ print('connection lost')
+ asyncio.get_event_loop().stop()
- def connection_lost(self, exc):
- print('connection lost:', exc)
- self.h_timeout.cancel()
+ loop = asyncio.get_event_loop()
+ task = loop.create_connection(EchoClient, '127.0.0.1', 8888)
+ loop.run_until_complete(task)
+ loop.run_forever()
+ loop.close()