-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
47 lines (39 loc) · 1.35 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
import socket
import threading
import common
class Client:
"""
client class will model the user
@__nickname: a name chosen by the user and sent to server
after ack message is recieved.
"""
def __init__(self, nickname):
self.__nickname = nickname
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self, host, port):
self.client.connect((host,port))
def receive(self):
while True:
try:
message = self.client.recv(1024).decode(common.ENCODING)
if message == common.KEYWORD:
self.client.send(self.__nickname.encode(common.ENCODING))
else:
print(f'{message} ')
except:
print('an error occured')
self.client.close()
break
def write(self):
while True:
message = f'{self.__nickname}: {input("")}'
self.client.send(message.encode(common.ENCODING))
def join_chat(self):
receive_thread = threading.Thread(target=self.receive)
receive_thread.start()
write_thread = threading.Thread(target=self.write)
write_thread.start()
nickname = input('Choose a nickname: ')
user = Client(nickname)
user.connect(common.HOST_NAME, common.PORT)
user.join_chat()