Skip to content
This repository was archived by the owner on May 31, 2021. It is now read-only.

Commit 1dcaece

Browse files
committed
TCP echo
1 parent 39b0016 commit 1dcaece

File tree

6 files changed

+70
-34
lines changed

6 files changed

+70
-34
lines changed

README.rst

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ Asyncio documentation
55
* GitHub: https://github.com/haypo/asyncio-doc
66
* AsyncIO documentation is written with `Sphinx <http://www.sphinx-doc.org/>`_.
77

8+
9+
Notes to writers
10+
================
11+
12+
Tutorials should use Python 3.5 ``async`` and ``await`` keywords rather than
13+
``yield from``.
14+
15+
816
Ideas
917
=====
1018

examples/tcp_echo_client.py

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
import asyncio
22

3-
class EchoClientProtocol(asyncio.Protocol):
4-
def __init__(self, message, loop):
5-
self.message = message
6-
self.loop = loop
73

8-
def connection_made(self, transport):
9-
transport.write(self.message.encode())
10-
print('Data sent: {!r}'.format(self.message))
4+
async def tcp_echo_client(message, loop):
5+
reader, writer = await asyncio.open_connection('127.0.0.1', 8888,
6+
loop=loop)
117

12-
def data_received(self, data):
13-
print('Data received: {!r}'.format(data.decode()))
8+
print('Send: %r' % message)
9+
writer.write(message.encode())
10+
11+
data = await reader.read(100)
12+
print('Received: %r' % data.decode())
13+
14+
print('Close the socket')
15+
writer.close()
1416

15-
def connection_lost(self, exc):
16-
print('The server closed the connection')
17-
print('Stop the event loop')
18-
self.loop.stop()
1917

20-
loop = asyncio.get_event_loop()
2118
message = 'Hello World!'
22-
coro = loop.create_connection(lambda: EchoClientProtocol(message, loop),
23-
'127.0.0.1', 8888)
24-
loop.run_until_complete(coro)
25-
loop.run_forever()
19+
loop = asyncio.get_event_loop()
20+
loop.run_until_complete(tcp_echo_client(message, loop))
2621
loop.close()

examples/tcp_echo_server.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import asyncio
2+
3+
async def handle_echo(reader, writer):
4+
data = await reader.read(100)
5+
message = data.decode()
6+
addr = writer.get_extra_info('peername')
7+
print("Received %r from %r" % (message, addr))
8+
9+
print("Send: %r" % message)
10+
writer.write(data)
11+
await writer.drain()
12+
13+
print("Close the client socket")
14+
writer.close()
15+
16+
loop = asyncio.get_event_loop()
17+
coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop)
18+
server = loop.run_until_complete(coro)
19+
20+
# Serve requests until Ctrl+C is pressed
21+
print('Serving on {}'.format(server.sockets[0].getsockname()))
22+
try:
23+
loop.run_forever()
24+
except KeyboardInterrupt:
25+
pass
26+
27+
# Close the server
28+
server.close()
29+
loop.run_until_complete(server.wait_closed())
30+
loop.close()

index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Chapiter 1: First steps with asyncio
1212
hello_world.rst
1313
hello_clock.rst
1414
http_client.rst
15-
tcp_echo_client.rst
1615
twisted.rst
1716
getting_help.rst
1817

@@ -23,6 +22,7 @@ Chapiter 2: Advanced topics
2322
.. toctree::
2423
:maxdepth: 2
2524

25+
tcp_echo.rst
2626
threads.rst
2727
producer_consumer.rst
2828
debug_mode.rst

tcp_echo.rst

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
++++++++++++++++++++++++++
2+
TCP echo client and server
3+
++++++++++++++++++++++++++
4+
5+
TCP echo client
6+
---------------
7+
8+
TCP echo client using streams:
9+
10+
.. literalinclude:: examples/tcp_echo_client.py
11+
12+
13+
TCP echo server
14+
---------------
15+
16+
TCP echo server using streams:
17+
18+
.. literalinclude:: examples/tcp_echo_server.py

tcp_echo_client.rst

-15
This file was deleted.

0 commit comments

Comments
 (0)