-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathtcp-server.py
59 lines (41 loc) · 1.29 KB
/
tcp-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
# coding:utf-8
import signal
import socket
import time
import threading
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, signum, frame):
self.kill_now = True
def connection_handler(client, address):
print('Accept new connection from %s:%s...' % address)
while True:
if not client:
break
data = client.recv(1024)
if not data:
client.close()
break
if killer.kill_now:
client.close()
break
client.send(data)
print("message:", data.decode('utf-8').encode('utf-8'))
time.sleep(1)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 12345))
server.listen(5) # 只监听5个连接,不传值则监听任意多个连接
killer = GracefulKiller()
def main():
while True:
client, address = server.accept()
t = threading.Thread(target=connection_handler, args=(client, address))
t.start()
if killer.kill_now:
client.close()
break
if __name__ == "__main__":
main()