-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_server.py
More file actions
27 lines (19 loc) · 758 Bytes
/
Copy pathecho_server.py
File metadata and controls
27 lines (19 loc) · 758 Bytes
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
26
import asyncio
class EchoServerProtocol(asyncio.Protocol):
def __init__(self, cb_session=None):
super().__init__()
self.cb_session = cb_session
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
print('Connection from {}'.format(peername))
self.transport = transport
def data_received(self, data):
message = data.decode()
print('Data received: {!r}'.format(message))
# publish received data to crossbar
if self.cb_session:
self.cb_session.publish("tcp.data", message)
print('Send: {!r}'.format(message))
self.transport.write(data)
print('Close the client socket')
self.transport.close()