Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed05159

Browse files
committed
asyncio doc: add TCP echo client/server using streams
1 parent 7567865 commit ed05159

2 files changed

Lines changed: 102 additions & 8 deletions

File tree

Doc/library/asyncio-protocol.rst

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,13 @@ coroutine can be used to wait until the write buffer is flushed.
439439
Protocol examples
440440
=================
441441

442-
TCP echo client
443-
---------------
442+
.. _asyncio-tcp-echo-client-protocol:
444443

445-
TCP echo client example, send data and wait until the connection is closed::
444+
TCP echo client protocol
445+
------------------------
446+
447+
TCP echo client using the :meth:`BaseEventLoop.create_connection` method, send
448+
data and wait until the connection is closed::
446449

447450
import asyncio
448451

@@ -478,11 +481,19 @@ having to write a short coroutine to handle the exception and stop the
478481
running loop. At :meth:`~BaseEventLoop.run_until_complete` exit, the loop is
479482
no longer running, so there is no need to stop the loop in case of an error.
480483

484+
.. seealso::
485+
486+
The :ref:`TCP echo client using streams <asyncio-tcp-echo-client-streams>`
487+
example uses the :func:`asyncio.open_connection` function.
488+
481489

482-
TCP echo server
483-
---------------
490+
.. _asyncio-tcp-echo-server-protocol:
484491

485-
TCP echo server example, send back received data and close the connection::
492+
TCP echo server protocol
493+
------------------------
494+
495+
TCP echo server using the :meth:`BaseEventLoop.create_server` method, send back
496+
received data and close the connection::
486497

487498
import asyncio
488499

@@ -507,12 +518,12 @@ TCP echo server example, send back received data and close the connection::
507518
coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
508519
server = loop.run_until_complete(coro)
509520

510-
# Server requests until CTRL+c is pressed
521+
# Serve requests until CTRL+c is pressed
511522
print('Serving on {}'.format(server.sockets[0].getsockname()))
512523
try:
513524
loop.run_forever()
514525
except KeyboardInterrupt:
515-
print("exit")
526+
pass
516527

517528
# Close the server
518529
server.close()
@@ -524,6 +535,11 @@ TCP echo server example, send back received data and close the connection::
524535
methods are asynchronous. ``yield from`` is not needed because these transport
525536
methods are not coroutines.
526537

538+
.. seealso::
539+
540+
The :ref:`TCP echo server using streams <asyncio-tcp-echo-server-streams>`
541+
example uses the :func:`asyncio.start_server` function.
542+
527543

528544
.. _asyncio-udp-echo-client-protocol:
529545

Doc/library/asyncio-stream.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,84 @@ IncompleteReadError
241241
Stream examples
242242
===============
243243

244+
.. _asyncio-tcp-echo-client-streams:
245+
246+
TCP echo client using streams
247+
-----------------------------
248+
249+
TCP echo client using the :func:`asyncio.open_connection` function::
250+
251+
import asyncio
252+
253+
def tcp_echo_client(message, loop):
254+
reader, writer = yield from asyncio.open_connection('127.0.0.1', 8888,
255+
loop=loop)
256+
257+
print('Send: %r' % message)
258+
writer.write(message.encode())
259+
260+
data = yield from reader.read(100)
261+
print('Received: %r' % data.decode())
262+
263+
print('Close the socket')
264+
writer.close()
265+
266+
message = 'Hello World!'
267+
loop = asyncio.get_event_loop()
268+
loop.run_until_complete(tcp_echo_client(message, loop))
269+
loop.close()
270+
271+
.. seealso::
272+
273+
The :ref:`TCP echo client protocol <asyncio-tcp-echo-client-protocol>`
274+
example uses the :meth:`BaseEventLoop.create_connection` method.
275+
276+
277+
.. _asyncio-tcp-echo-server-streams:
278+
279+
TCP echo server using streams
280+
-----------------------------
281+
282+
TCP echo server using the :func:`asyncio.start_server` function::
283+
284+
import asyncio
285+
286+
@asyncio.coroutine
287+
def handle_echo(reader, writer):
288+
data = yield from reader.read(100)
289+
message = data.decode()
290+
addr = writer.get_extra_info('peername')
291+
print("Received %r from %r" % (message, addr))
292+
293+
print("Send: %r" % message)
294+
writer.write(data)
295+
yield from writer.drain()
296+
297+
print("Close the client socket")
298+
writer.close()
299+
300+
loop = asyncio.get_event_loop()
301+
coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop)
302+
server = loop.run_until_complete(coro)
303+
304+
# Serve requests until CTRL+c is pressed
305+
print('Serving on {}'.format(server.sockets[0].getsockname()))
306+
try:
307+
loop.run_forever()
308+
except KeyboardInterrupt:
309+
pass
310+
311+
# Close the server
312+
server.close()
313+
loop.run_until_complete(server.wait_closed())
314+
loop.close()
315+
316+
.. seealso::
317+
318+
The :ref:`TCP echo server protocol <asyncio-tcp-echo-server-protocol>`
319+
example uses the :meth:`BaseEventLoop.create_server` method.
320+
321+
244322
Get HTTP headers
245323
----------------
246324

0 commit comments

Comments
 (0)