-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
74 lines (59 loc) · 1.61 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import socket
import threading
import sqlite3
HOST_IP = socket.gethostbyname(socket.gethostname())
HOST_PORT = 12345
ENCODER = "utf-8"
BYTESIZE = 1024
connect_db = sqlite3.connect(r'db\info.db')
cur = sqlite3.Cursor(connect_db)
server = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
server.bind((HOST_IP , HOST_PORT))
server.listen()
clients = []
nicknames = []
db = []
for row in cur.execute('SELECT Username , Password from chillzone'):
db.append(row)
def brodcast(message):
for client in clients:
client.send(message)
def valid(client):
flag = False
while True:
try:
message = client.recv(BYTESIZE).decode(ENCODER).strip()
newmessage = message.split(":")
info = (newmessage[0] , newmessage[1])
if (info in db)and newmessage[0] not in nicknames:
client.send('enter'.encode(ENCODER))
nicknames.append(newmessage[0])
clients.append(client)
flag = True
break
else:
print("EROR")
print(message)
client.send('info'.encode(ENCODER))
except:
client.close()
break
while flag:
try :
message = client.recv(BYTESIZE)
brodcast(message)
except:
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
nicknames.remove(nickname)
brodcast(f'{nickname}:left the chat'.encode(ENCODER))
break
def recieve():
while True:
client , add = server.accept()
client.send('info'.encode(ENCODER))
vt = threading.Thread(target=valid , args=(client,))
vt.start()
recieve()