-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.py
executable file
·61 lines (50 loc) · 1.65 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import socket
import threading
from threading import Thread # library for multi-threading
#HEADER = 64
#PORT = 5050
FORMAT = 'utf-8'
#SERVER = "10.42.0.21"#"127.0.1.1"
#ADDR = (SERVER, PORT)
DISCONNECT_MESSAGE = "!DISCONNECT"
class Client:
def __init__(self, HEADER = 64, PORT = 5050, SERVER = "10.42.0.21"):
self.mesg = "Hello!" # message to send to server
self.HEADER = HEADER
self.PORT = PORT
self.SERVER = SERVER
self.stopped = False
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# thread instantiation
#self.t = Thread(target=self.send, args=())
#self.t.daemon = True # daemon threads run in background
def start(self):
ADDR = (self.SERVER, self.PORT)
self.client.connect(ADDR)
#self.t.start()
self.send()
def get_message(self, info):
self.mesg = info
self.send()
def send(self):
message = self.mesg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (self.HEADER - len(send_length))
self.client.send(send_length)
self.client.send(message)
def stop(self):
self.get_message(DISCONNECT_MESSAGE)
print(self.client.recv(2048).decode(FORMAT))
#self.t.join()
if __name__ == '__main__':
check = Client(HEADER = 64, PORT = 5050, SERVER = "10.42.0.21")
check.start()
check.get_message("Hello World!")
while True:
mes = input()
if mes == "q":
check.get_message(DISCONNECT_MESSAGE)
break
else:
check.get_message(mes)