summaryrefslogtreecommitdiffstats
path: root/Doc/library/asyncio-protocol.rst
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-10-12 09:13:40 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-10-12 09:13:40 (GMT)
commitc2721b41d38cf9f95009e0aa16a07cfea432de57 (patch)
tree97eda42588c1a3d1ab7ee23fa715901627ba986d /Doc/library/asyncio-protocol.rst
parent0e34dc37379e3c42b19c30410e190966e65f4dad (diff)
downloadcpython-c2721b41d38cf9f95009e0aa16a07cfea432de57.zip
cpython-c2721b41d38cf9f95009e0aa16a07cfea432de57.tar.gz
cpython-c2721b41d38cf9f95009e0aa16a07cfea432de57.tar.bz2
asyncio doc: use server.wait_closed() in TCP echo server example
Diffstat (limited to 'Doc/library/asyncio-protocol.rst')
-rw-r--r--Doc/library/asyncio-protocol.rst21
1 files changed, 14 insertions, 7 deletions
diff --git a/Doc/library/asyncio-protocol.rst b/Doc/library/asyncio-protocol.rst
index 52da1b4..ec6a479 100644
--- a/Doc/library/asyncio-protocol.rst
+++ b/Doc/library/asyncio-protocol.rst
@@ -473,6 +473,7 @@ having to write a short coroutine to handle the exception and stop the
running loop. At :meth:`~BaseEventLoop.run_until_complete` exit, the loop is
no longer running, so there is no need to stop the loop in case of an error.
+
TCP echo server
---------------
@@ -483,28 +484,34 @@ TCP echo server example, send back received data and close the connection::
class EchoServer(asyncio.Protocol):
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
- print('connection from {}'.format(peername))
+ print('Connection from {}'.format(peername))
self.transport = transport
def data_received(self, data):
- print('data received: {}'.format(data.decode()))
+ message = data.decode()
+ print('Data received: {!r}'.format(message))
+
+ print('Send: {!r}'.format(message))
self.transport.write(data)
- # close the socket
+ print('Close the socket')
self.transport.close()
loop = asyncio.get_event_loop()
coro = loop.create_server(EchoServer, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)
- print('serving on {}'.format(server.sockets[0].getsockname()))
+ # Server requests until CTRL+c is pressed
+ print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
print("exit")
- finally:
- server.close()
- loop.close()
+
+ # Close the server
+ server.close()
+ loop.run_until_complete(server.wait_closed())
+ loop.close()
:meth:`Transport.close` can be called immediately after
:meth:`WriteTransport.write` even if data are not sent yet on the socket: both