-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
39 lines (31 loc) · 908 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
37
38
39
import time
from threading import Thread
import socket
HOST = socket.gethostbyname(socket.gethostname())
PORT = 5000
ADDR = (HOST,PORT)
BUFSIZ = 1024
MAX_CONNECTIONS = 10
class Client:
def __init__(self):
self.client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.client_socket.connect(ADDR)
reveive_thread = Thread(target = self.receive_messages)
reveive_thread.start()
def receive_messages(self):
while True:
try:
msg = self.client_socket.recv(BUFSIZ).decode('utf8')
if msg.lower() == "quit":
self.terminting_socket()
else:
print(msg)
return msg
except Exception as e:
terminting_socket()
def send_messages(self,msg):
time.sleep(2)
self.client_socket.send(bytes("client:"+str(msg),'utf8'))
def terminting_socket(self):
print("communication gateway closed")
self.client_socket.close()