-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
57 lines (51 loc) · 1.68 KB
/
Copy pathclient.py
File metadata and controls
57 lines (51 loc) · 1.68 KB
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
48
49
50
51
52
53
54
55
56
57
from socket import AF_INET, socket, SOCK_STREAM, gethostname, gethostbyname
from threading import Thread
class Client:
"""
for communication with the server
"""
HOST = gethostbyname(gethostname())
PORT = 7560
ADDRESS = (HOST, PORT)
BUFFER_SIZE = 512
__QUIT_CMD = '{/%quit%disconnect_now%/}'
def __init__(self, name, chat_box):
"""
Initialize object and send name to server
:param name: str
"""
self.num_of_msg = 0
self.client_socket = socket(AF_INET, SOCK_STREAM)
self.client_socket.connect(self.ADDRESS)
self.chat_box = chat_box
receive_thread = Thread(target=self.receive_messages)
receive_thread.start()
self.send_messages(name)
def receive_messages(self):
"""
receive messages from the server
:return: None
"""
while True:
try:
msg = self.client_socket.recv(self.BUFFER_SIZE).decode()
self.num_of_msg += 1
placing = float(self.num_of_msg)
msg = msg + '\n'
self.chat_box.config(state='normal')
self.chat_box.insert(placing, msg, ('left_align',))
self.chat_box.config(state='disabled')
except Exception as e:
print("[EXCEPTION]", e)
break
def send_messages(self, msg):
"""
send messages to the server
:param msg: str
:return: None
"""
self.client_socket.send(bytes(msg, 'utf8'))
if msg.__eq__(self.__QUIT_CMD):
self.client_socket.close()
def disconnect(self):
self.send_messages(self.__QUIT_CMD)