-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver_asyncio.py
More file actions
38 lines (30 loc) · 840 Bytes
/
server_asyncio.py
File metadata and controls
38 lines (30 loc) · 840 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
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
#coding=utf-8
"""
code from:
https://docs.python.org/3/library/asyncio-protocol.html#echo-server
"""
import asyncio
#import os
class EchoServer(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
def data_received(self, data):
if data is None:
return;
self.transport.write(data)
if __name__ == '__main__':
import sys
port = int(sys.argv[1]) if len(sys.argv) > 1 else 5000
loop = asyncio.get_event_loop()
coro = loop.create_server(EchoServer, '0.0.0.0', port)
server = loop.run_until_complete(coro)
#os.fork()
print('Starting %s on %d' % ( __file__, port))
try:
loop.run_forever()
except KeyboardInterrupt:
print("exit")
finally:
server.close()
loop.close()