-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol_server.py
More file actions
50 lines (35 loc) · 1.27 KB
/
Copy pathprotocol_server.py
File metadata and controls
50 lines (35 loc) · 1.27 KB
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
import asyncio
import time
OUTTIME = 10
clients = {}
class ProtocolServer(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
clients.setdefault(transport, time.time()+OUTTIME)
def data_received(self, data):
clients.update({self.transport: time.time()+OUTTIME}) # 一旦接受到数据就更新timeout时间
message = data.decode() # 回显发上来的数据
self.transport.write(message.encode())
async def check_alive(): # 如果timeout就通过对应的transport将客户端关闭
while True:
await asyncio.sleep(0.3)
for transport, timeout in clients.copy().items():
if timeout < time.time():
print(transport, timeout, time.time())
transport.close()
clients.pop(transport)
def main():
loop = asyncio.get_event_loop()
loop.set_debug(True)
coro = loop.create_server(ProtocolServer, '127.0.0.1', 8881)
asyncio.ensure_future(coro)
asyncio.ensure_future(check_alive(), loop=loop)
try:
loop.run_forever()
except Exception as e:
print(e)
finally:
loop.stop()
loop.close()
if __name__ == '__main__':
main()