-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
44 lines (37 loc) · 1.17 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
import socket
import pickle
import time
class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = "localhost"
self.port = 5555
self.addr = (self.host, self.port)
self.board = self.connect()
self.board = pickle.loads(self.board)
def connect(self):
self.client.connect(self.addr)
return self.client.recv(4096*8)
def disconnect(self):
self.client.close()
def send(self, data, pick=False):
"""
:param data: str
:return: str
"""
start_time = time.time()
while time.time() - start_time < 5:
try:
if pick:
self.client.send(pickle.dumps(data))
else:
self.client.send(str.encode(data))
reply = self.client.recv(4096*8)
try:
reply = pickle.loads(reply)
break
except Exception as e:
print(e)
except socket.error as e:
print(e)
return reply