-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
51 lines (39 loc) · 1.31 KB
/
client.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
import socket,threading,time
def recv_msg(sock) -> None:
"""
Recebe as mensagens dos outros clientes através do servidor
Args:
sock : socket do cliente
"""
connection = sock
try:
while True:
data = connection.recv(2000)
if data:
print(data.decode())
except Exception as e:
raise e
def client(host = "localhost", port=6969) -> None:
"""
Processo principal, são definidas as características do socket,
ele é iniciado, fica recebendo as mensagens do servidor e enviando
mensagens para o servidor
Args:
host (srt): ip do servidor
port (int): porta que deve ser usada
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (host, port)
try:
sock.connect(server_address)
print("CONENCTED!")
recive_msg = threading.Thread(target=recv_msg,args=[sock])
recive_msg.start()
while True:
t = time.strftime("%H:%M:%S",time.localtime())
msg = f"<{t}> {input()}"
sock.send(msg.encode())
sock.close()
except Exception as e:
print("Houve um problema: ",e)
client()