-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
37 lines (31 loc) · 976 Bytes
/
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
import socket
import threading
clients = []
def broadcast(message, client_socket):
for client in clients:
if client != client_socket:
try:
client.send(message)
except:
client.close()
clients.remove(client)
def handle_client(client_socket):
while True:
try:
message = client_socket.recv(1024)
broadcast(message, client_socket)
except:
client_socket.close()
clients.remove(client_socket)
break
def receive_connections():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('0.0.0.0', 5555))
server.listen()
print("Server started")
while True:
client_socket, client_address = server.accept()
clients.append(client_socket)
threading.Thread(target=handle_client, args=(client_socket,)).start()
if __name__ == "__main__":
receive_connections()