Example udp_server.pyΒΆ

 1# Copyright (c) 2012 Denis Bilenko. See LICENSE for details.
 2"""A simple UDP server.
 3
 4For every message received, it sends a reply back.
 5
 6You can use udp_client.py to send a message.
 7"""
 8from __future__ import print_function
 9from gevent.server import DatagramServer
10
11
12class EchoServer(DatagramServer):
13
14    def handle(self, data, address): # pylint:disable=method-hidden
15        print('%s: got %r' % (address[0], data))
16        self.socket.sendto(('Received %s bytes' % len(data)).encode('utf-8'), address)
17
18
19if __name__ == '__main__':
20    print('Receiving datagrams on :9000')
21    EchoServer(':9000').serve_forever()

Current source

Next page: Example unixsocket_client.py