Echo Server and Client¶
This example illustrates how to write a simple TCP Echo server and client pair.
The example is simple because the client and server protocols are symmetrical
and therefore the
EchoProtocol will also be used as based class for
The code for this example is located in the
Run The example¶
To run the server:
Open a new shell, in this directory, launch python and type:
>>> from manage import Echo >>> echo = Echo(('localhost',8060)) >>> echo(b'Hello!') b'Hello!'
Writing the Client¶
The first step is to write a small class handling a connection
pool with the remote server. The
Echo class does just that,
it subclasses the handy
AbstractClient and uses
Pool of connections as backbone.
- It encodes and sends the request to the remote server via the
- It listens for incoming data from the remote server via the
To wait for the response message one can
await from the
Echo Client Protocol¶
EchoProtocol(loop=None, one_time_events=None, many_times_events=None)¶
ProtocolConsumerfor client and servers.
The only difference between client and server is the implementation of the
It simply search for the
separatorand, if found, it invokes the
response()method with the value of the message.
Clients return the message so that the
ProtocolConsumer.on_finishedis called back with the message value, while servers sends the message back to the client.
Echo Server Protocol¶
Echo(address, full_response=False, pool_size=10, loop=None)¶
A client for the echo server.
The event loop used by the client IO requests.
The event loop is stored at this attribute so that asynchronous method decorators such as
task()can be used.
remote server TCP address.
Flag indicating if the callable method should return the
EchoProtocolhandling the request (
True) or the server response message (
messageto the server and wait for a response.