-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_chat.py
42 lines (35 loc) · 1.04 KB
/
server_chat.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
import socket
host = '127.0.0.1'
port = 3000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
try:
sock.bind((host, port))
print('Welcome to Bony Chat Server ')
break
except OSError:
port += 1
usr = {}
while True:
data, addr = sock.recvfrom(1024)
print('Date Received')
if data == '':
continue
user_id = addr[1]
data = data.decode()
if data == 'enter':
print(f'Client {user_id} enter the chat')
continue
if '<nick_check>' in data:
user_nick = data.split('>=')[1]
if user_nick not in usr.values():
usr.setdefault(addr, user_nick)
sock.sendto('<nick_check_true>'.encode(), addr)
continue
else:
sock.sendto('<nick_check_false>'.encode(), addr)
continue
data = f'{usr.get(addr)}: {data}'
for user in usr:
if user != addr:
sock.sendto(data.encode(), user)