Example 3: echo server, using a custom ProtocolΒΆ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Gruvi example program: an echo server, using a Protocol

import gruvi

class EchoProtocol(gruvi.Protocol):

    def connection_made(self, transport):
        super(EchoProtocol, self).connection_made(transport)
        peer = transport.get_extra_info('peername')
        print('New connection from {0}'.format(gruvi.saddr(peer)))

    def data_received(self, data):
        self._transport.write(data)

    def eof_received(self):
        print('Connection lost')

server = gruvi.create_server(EchoProtocol, ('localhost', 0))
for addr in server.addresses:
    print('Listen on {0}'.format(gruvi.saddr(addr)))

try:
    gruvi.get_hub().switch()
except KeyboardInterrupt:
    print('Exiting on CTRL-C')

Previous topic

Example 2: echo server, using a StreamServer

Next topic

Example 4: netcat client