-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-server.py
More file actions
34 lines (27 loc) · 860 Bytes
/
chat-server.py
File metadata and controls
34 lines (27 loc) · 860 Bytes
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
import socket
#Porta do servidor
port = int(input('port = '))
#Objeto socket
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Associa o socket a uma porta local
serv.bind(('0.0.0.0',port))
serv.listen()
while True:
#Servidor fica aguardando conexões
print(f'== Servidor aguardando conexões na porta {port} ==')
conn, addr = serv.accept()
print(f'== Conexao recebida de {addr} ==')
while True:
#Recebe os dados
data = conn.recv(4096)
from_client = data
print('Cliente: ', from_client.decode())
if from_client.decode() == 'logout':
conn.close()
print('== Cliente desconectado ==')
break
else:
#Mensagem que o servidor envia
msg = input('Server: ')
#Envia uma mensagem
conn.send(msg.encode())