-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
35 lines (29 loc) · 836 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
import socket
import threading
HOST = '127.0.0.1'
PORT = 14532
username = input("Digite seu nome de usuário: ")
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))
def receive():
while True:
try:
message = client.recv(1024).decode('utf-8')
if message == 'USERNAME':
client.send(username.encode('utf-8'))
else:
print(message)
except:
print("Encerrando conexão com o servidor...")
client.close()
break
def write():
while True:
try:
message = input("")
client.send(message.encode('utf-8'))
except KeyboardInterrupt:
break
receive_thread = threading.Thread(target=receive, daemon=True)
receive_thread.start()
write()