-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
49 lines (38 loc) · 1.35 KB
/
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
46
47
48
49
import _thread
import socket
import json
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
host="localhost"
port=5000
s.bind((host,port))
s.listen(5)
clients=[]
clients_name = []
def connectNewClient(c):
while True:
try:
msg = c.recv(2048).decode('utf-8')
j = msg.replace("'","\"")
d = json.loads(j)
print(d)
if d['alert'] == 'Online':
clients_name.append(d['username'])
elif d['alert'] == 'Offline':
clients_name.pop(clients_name.index(d['username']))
clients.pop(clients.index(c))
to_send_dict = str({'user_list':clients_name,'alert': d['alert'],'message': d['message'],'username':d['username']})
sendToAll(to_send_dict)
except:
pass
def sendToAll(msg):
for client in clients:
client.send(msg.encode('utf-8'))
while True:
try:
c,ad=s.accept()
print('Connection Established')
clients.append(c)
_thread.start_new_thread(connectNewClient,(c,))
except:
continue