-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
36 lines (28 loc) · 913 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
import socket
from threading import Thread
nickname = input("Choose your nickname: ")
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip_address = '127.0.0.1'
port = 8000
client.connect((ip_address, port))
print("Connected with the server...")
def receive():
while True:
try:
message = client.recv(2048).decode('utf-8')
if message == "NICKNAME":
client.send(nickname.encode('utf-8'))
else:
print(message)
except:
print("An error occured!")
client.close
break
def write():
while True:
message = '{}: {}'.format(nickname, input(''))
client.send(message.encode('utf-8'))
receive_thread = Thread(target=receive)
receive_thread.start()
write_thread = Thread(target=write)
write_thread.start()