-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
81 lines (61 loc) · 2.25 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
75
76
77
78
79
80
81
import socket
import threading
class Server:
def __init__(self, host, port):
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = host
self.port = port
self.clients = []
self.nicknames = []
def connect(self):
self.connection.bind((self.host, self.port))
self.connection.listen()
print("Servidor inicializado com sucesso!")
def disconnect(self):
self.connection.close()
def broadcast(self, current_client, message):
for client in self.clients:
if client != current_client:
if type(message) is str:
formatted_message = (str(message) + '\n').encode('UTF8')
if type(message) is bytes:
formatted_message = message
client.send(formatted_message)
def send_message(self, client, message):
formatted_message = (str(message) + '\n').encode('UTF8')
client.send(formatted_message)
def connect_client(self, client):
nickname = client.recv(1024).decode('UTF8')
self.nicknames.append(nickname)
self.clients.append(client)
self.broadcast(client, f'{nickname} entrou no chat!')
self.send_message(client, 'Conectado no servidor!')
def disconnect_client(self, client):
index = self.clients.index(client)
self.clients.remove(client)
client.close()
nickname = self.nicknames[index]
message = f"{nickname} saiu do chat!"
self.broadcast(client, message)
self.nicknames.remove(nickname)
def handle(self, client):
while True:
try:
message = client.recv(1024)
self.broadcast(client, message)
except:
self.disconnect_client(client)
break
def receive(self):
while True:
client, address = self.connection.accept()
print(f'Conectado com {address[0]}:{address[1]}')
self.connect_client(client)
thread = threading.Thread(target=self.handle, args=[client])
thread.start()
def main():
server = Server('0.0.0.0', 7976)
server.connect()
server.receive()
if __name__ == "__main__":
main()