-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
50 lines (39 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
50
import socket
import threading
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '127.0.0.1'
PORT = 14532
server.bind((HOST, PORT))
server.listen()
print(f"Server escutando na porta: {PORT}")
clients = []
usernames = []
def sendMessage(message):
for client in clients:
client.send(message)
def handle(client, username):
while True:
try:
message = client.recv(1024).decode('utf-8')
sendMessage(f"[{username}] {message}".encode('utf-8'))
except:
index = clients.index(client)
clients.remove(client)
client.close()
username = usernames[index]
usernames.remove(username)
sendMessage(f"{username} desconectou do servidor.".encode('utf-8'))
break
def receive():
while True:
client, address = server.accept()
client.send("USERNAME".encode('utf-8'))
username = client.recv(1024).decode('utf-8')
usernames.append(username)
print(f"O usuário {username} se conectou no servidor! endereço: {address}")
sendMessage(f"{username} entrou no chat.".encode('utf-8'))
clients.append(client)
client.send("Conectado ao servidor".encode('utf-8'))
thread = threading.Thread(target=handle, args=(client, username, ), daemon=True)
thread.start()
receive()