diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-10-12 18:18:16 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-10-12 18:18:16 (GMT) |
commit | ed051594d70fe4c6b170aee667af4e32dfe6b867 (patch) | |
tree | 289297c1e593b46e60106d20b388eb24e4dc70ba /Doc/library/asyncio-stream.rst | |
parent | 7567865867968ddc9e914a15354e4accb417513a (diff) | |
download | cpython-ed051594d70fe4c6b170aee667af4e32dfe6b867.zip cpython-ed051594d70fe4c6b170aee667af4e32dfe6b867.tar.gz cpython-ed051594d70fe4c6b170aee667af4e32dfe6b867.tar.bz2 |
asyncio doc: add TCP echo client/server using streams
Diffstat (limited to 'Doc/library/asyncio-stream.rst')
-rw-r--r-- | Doc/library/asyncio-stream.rst | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/Doc/library/asyncio-stream.rst b/Doc/library/asyncio-stream.rst index c3bbd20..1e6dc94 100644 --- a/Doc/library/asyncio-stream.rst +++ b/Doc/library/asyncio-stream.rst @@ -241,6 +241,84 @@ IncompleteReadError Stream examples =============== +.. _asyncio-tcp-echo-client-streams: + +TCP echo client using streams +----------------------------- + +TCP echo client using the :func:`asyncio.open_connection` function:: + + import asyncio + + def tcp_echo_client(message, loop): + reader, writer = yield from asyncio.open_connection('127.0.0.1', 8888, + loop=loop) + + print('Send: %r' % message) + writer.write(message.encode()) + + data = yield from reader.read(100) + print('Received: %r' % data.decode()) + + print('Close the socket') + writer.close() + + message = 'Hello World!' + loop = asyncio.get_event_loop() + loop.run_until_complete(tcp_echo_client(message, loop)) + loop.close() + +.. seealso:: + + The :ref:`TCP echo client protocol <asyncio-tcp-echo-client-protocol>` + example uses the :meth:`BaseEventLoop.create_connection` method. + + +.. _asyncio-tcp-echo-server-streams: + +TCP echo server using streams +----------------------------- + +TCP echo server using the :func:`asyncio.start_server` function:: + + import asyncio + + @asyncio.coroutine + def handle_echo(reader, writer): + data = yield from reader.read(100) + message = data.decode() + addr = writer.get_extra_info('peername') + print("Received %r from %r" % (message, addr)) + + print("Send: %r" % message) + writer.write(data) + yield from writer.drain() + + print("Close the client socket") + writer.close() + + loop = asyncio.get_event_loop() + coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop) + server = loop.run_until_complete(coro) + + # Serve requests until CTRL+c is pressed + print('Serving on {}'.format(server.sockets[0].getsockname())) + try: + loop.run_forever() + except KeyboardInterrupt: + pass + + # Close the server + server.close() + loop.run_until_complete(server.wait_closed()) + loop.close() + +.. seealso:: + + The :ref:`TCP echo server protocol <asyncio-tcp-echo-server-protocol>` + example uses the :meth:`BaseEventLoop.create_server` method. + + Get HTTP headers ---------------- |