-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
36 lines (29 loc) · 865 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
import threading
PORT = 5050
HOST = socket.gethostbyname(socket.gethostname())
nickname = input('Choose a nickname: ')
ADDR = (HOST, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
#receive data from server
def receive():
while True:
try:
massage = client.recv(1024).decode('ascii')
if massage == 'NICK':
client.send(nickname.encode('ascii'))
else:
print(massage)
except:
print('An error occured!')
client.close()
break
def write():
while True:
massage = f'{nickname}: {input(">>> ")}'
client.send(massage.encode('ascii'))
receive_thread = threading.Thread(target=receive)
receive_thread.start()
write_thread = threading.Thread(target=write)
write_thread.start()