-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
46 lines (42 loc) · 1.57 KB
/
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
import socket
import json
import threading
class Server:
def __init__(self, port:int=5000):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((socket.gethostname(), port))
self.clients:dict[str, socket.socket] = {}
self.contacts:list = []
def client(self, socket:socket.socket, address:str):
while True:
try:
msg = socket.recv(4096)
except:
break
if msg == b"":
break
msg = json.loads(msg)
if "msg" in msg:
for client in self.clients:
self.clients[client].send(bytes(json.dumps({"msg": msg["msg"], "mac": msg["mac"]}), "utf-8"))
for contact in self.contacts:
if contact["mac"] == address:
self.contacts.remove(contact)
break
del self.clients[address]
def start(self):
self.socket.listen(5)
while True:
clientsocket, _ = self.socket.accept()
clientsocket.send(bytes(json.dumps(self.contacts), "utf-8"))
log = clientsocket.recv(4096)
log = json.loads(log)
print(log)
for client in self.clients:
self.clients[client].send(bytes(json.dumps({"new": log}), "utf-8"))
self.clients[log["mac"]] = clientsocket
self.contacts.append(log)
threading.Thread(target=self.client, args=(clientsocket, log["mac"])).start()
if __name__ == "__main__":
server = Server()
server.start()