-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
36 lines (30 loc) · 846 Bytes
/
server.py
File metadata and controls
36 lines (30 loc) · 846 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
import socket
from threading import Thread
from debugpy import listen
host = "0.0.0.0"
port = 8080
separator_token = '<SEP>'
client_sockets = set()
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(5)
def listen_for_clients(cs):
while True:
try:
msg = cs.recv(1024).decode()
except Exception as e:
print(f'error: {e}')
client_sockets.remove(cs)
else:
msg = msg.replace(separator_token, ': ')
for client in client_sockets:
client.send(msg.encode())
while True:
client_socket, address = s.accept()
client_sockets.add(client_socket)
t = Thread(target = listen_for_clients, args = (client_socket,), daemon=True)
t.start()
for cs in client_sockets:
cs.close()
s.close()