forked from f-prime/CentralChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
31 lines (29 loc) · 916 Bytes
/
Copy pathserver.py
File metadata and controls
31 lines (29 loc) · 916 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
import socket
import threading
class CentralServer:
def __init__(self):
self.nodes = []
self.sock = socket.socket()
def main(self):
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(("0.0.0.0", 5124))
self.sock.listen(5)
while True:
obj, conn = self.sock.accept()
self.nodes.append(obj)
threading.Thread(target=self.handle, args=(obj,)).start()
def handle(self, obj):
while True:
data = obj.recv(1024)
if not data:
self.nodes.remove(obj)
break
else:
for x in self.nodes:
try:
x.send(data)
except:
self.nodes.remove(x)
continue
if __name__ == "__main__":
CentralServer().main()