Udp Server and Client¶
This example illustrates how to write a UDP Echo server and client pair.
The code for this example is located in the examples.echoudp.manage
module.
Run The example¶
To run the server:
python manage.py
Open a new shell, in this directory, launch python and type:
>>> from manage import Echo
>>> echo = Echo(('localhost', 8060))
>>> echo(b'Hello!\n')
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 subclass the handy AbstractUdpClient
and uses
the asynchronous Pool
of connections as backbone.
The second step is the implementation of the EchoUdpProtocol
,
a subclass of DatagramProtocol
.
The EchoUdpProtocol
is needed for two reasons:
- It encodes and sends the request to the remote server
- It listens for incoming data from the remote server via the
datagram_received()
method.
Implementation¶
Echo UDP Protocol¶
-
class
examples.echoudp.manage.
EchoUdpProtocol
(loop, session=1, producer=None, logger=None, **kw)[source]¶ A base
DatagramProtocol
for UDP echo clients and servers.The only difference between client and server is the implementation of the
response()
method.
Echo Client¶
-
class
examples.echoudp.manage.
Echo
(address, pool_size=5, loop=None)[source]¶ A client for the echo server.
Parameters: -
_loop
¶ 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.
-
address
¶ remote server UDP address.
-
Echo Server¶
-
examples.echoudp.manage.
server
(name=None, description=None, **kwargs)[source]¶ Create the
UdpSocketServer
.