-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaio_stateful_server.py
67 lines (49 loc) · 1.89 KB
/
aio_stateful_server.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# coding: utf-8
"""Stateful server based on python3 asyncio callback interface"""
import asyncio
# Each client connection will create a new protocol instance
class Proto(asyncio.Protocol):
def __init__(self, master):
print("Proto created")
self.master = master
super(Proto, self).__init__()
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))
if message.splitlines()[0] == "Done":
print('Got magic word')
print('Close the client socket')
self.transport.close() # Important, server won't close otherwise
self.master.server.close()
return
print('Send: {!r}'.format(message))
self.transport.write(data)
# Enable Proto access to common server instance
class RegServer:
def __init__(self, proto, host, port, loop):
self.host, self.port = host, port
self.proto = proto
self.loop = loop
self.server = loop.run_until_complete(
loop.create_server(self.proto_factory, host, port))
def proto_factory(self):
return self.proto(self)
loop = asyncio.get_event_loop()
# creator = loop.create_server(Proto, '127.0.0.1', 8888)
# loop.create_task(creator)
# server = loop.run_until_complete(loop.create_task(creator))
# server = loop.run_until_complete(creator)
reg = RegServer(Proto, '127.0.0.1', 8888, loop)
# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(reg.server.sockets[0].getsockname()))
try:
loop.run_until_complete(reg.server.wait_closed())
except KeyboardInterrupt:
reg.server.close()
loop.run_until_complete(reg.server.wait_closed())
loop.close()