-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
46 lines (34 loc) · 1.13 KB
/
server.py
File metadata and controls
46 lines (34 loc) · 1.13 KB
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 select
IP = "0.0.0.0"
PORT = 5353
server = socket.socket()
server.bind((IP, PORT))
server.listen(2)
client_list = []
messages_to_send = []
names={}
def send_waiting_message(wlist):
for message in messages_to_send:
src_client, msg = message
for client in wlist:
if client != src_client:
client.send(msg)
messages_to_send.remove(message)
while True:
rlist, wlist, xlist = select.select([server] + client_list, client_list, [])
for current_socket in rlist:
if current_socket is server:
(new_socket, address) = server.accept()
client_list.append(new_socket)
print("New connection with client!")
else:
data = current_socket.recv(1024)
print (data.decode("UTF-8"))
# data = current_socket.recv(rlist, wlist, xlist)
if data.decode("UTF-8") == "EXIT":
client_list.remove(current_socket)
print("Connection Closed")
else:
messages_to_send.append((current_socket, b"" + data))
send_waiting_message(wlist)