-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_server.py
45 lines (36 loc) · 1.18 KB
/
udp_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
import socket
import threading
import queue
HOST = 'localhost'
PORT = 9999
messages = queue.Queue()
clients = []
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind((HOST, PORT))
def receive():
while True:
try:
message, address = server.recvfrom(1024)
messages.put((message, address))
except:
pass
def broadcast():
while True:
while not messages.empty():
message, address = messages.get(1024)
print(message.decode())
if address not in clients:
clients.append(address)
for client in clients:
try:
if message.decode().startswith("SIGNUP_TAG:"):
nick = message.decode()[message.decode().index(":")+1:]
server.sendto(f'{nick} joined to the party!'.encode(), client)
else:
server.sendto(message, client)
except:
clients.remove(client)
thread_receive = threading.Thread(target=receive)
thread_broadcast = threading.Thread(target=broadcast)
thread_receive.start()
thread_broadcast.start()