-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
49 lines (36 loc) · 1.15 KB
/
client.py
File metadata and controls
49 lines (36 loc) · 1.15 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
import socket, pickle, json
from _thread import *
from importlib.resources import open_text
with open_text("communication", "config.json") as f:
config = json.load(f)
host = config["host"]
port = config["port"]
msg_length = config["msg length"]
class PSClient:
def __init__(self, id):
self.id = id
self.sock = socket.socket()
print("created socket successfully")
def connect(self, host=host, port=port):
self.sock.connect((host, port))
self.sock.send(str.encode(self.id))
msg = self.sock.recv(msg_length)
if msg.decode() != "connected":
print("disconnected")
return None
self.sock.send(str.encode("start"))
msg = self.sock.recv(msg_length)
if not msg:
print("disconnected")
return None
return pickle.loads(msg)
def step(self, action):
self.sock.send(pickle.dumps(action))
msg = self.sock.recv(msg_length)
if not msg:
print("disconnected")
return None
res = pickle.loads(msg)
return res
def close(self):
self.sock.close()