-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
44 lines (35 loc) · 1000 Bytes
/
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
import socket
import threading
import time
SERVER_IP = '127.0.1.1'
PORT = 5050
ADDR = (SERVER_IP, PORT)
FORMATO = 'utf-8'
#cliente e servidor se conectam basicamente mesma forma.
#porém ao invés de bind usa connect
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def handle_mensagens():
while True:
#No decode você não põe o formato, só no encode.
msg = client.recv(1024).decode()
mensagem = msg.split('=')
print(mensagem[1]+': '+mensagem[2])
def enviar(mensagem):
client.send(mensagem.encode(FORMATO))
def enviar_mensagem():
mensagem = input()
enviar("msg="+mensagem)
def enviar_nome():
nome = input('Digite seu nome: ')
enviar("nome="+nome)
def iniciar_envio():
enviar_nome()
while True:
enviar_mensagem()
def iniciar():
thread1 = threading.Thread(target=handle_mensagens)
thread2 = threading.Thread(target=iniciar_envio)
thread1.start()
thread2.start()
iniciar()