-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_tools.py
58 lines (42 loc) · 1.34 KB
/
client_tools.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
"""
Tools for communicating with the server with sockets
author: Teddy Tortorici
"""
import socket
import get
def send(msg: str, host: str = "localhost", port: int = get.port) -> str:
if msg:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# connect to server
s.connect((host, port))
# send message to server
s.send(msg.encode())
# get response from the server
msg_back = s.recv(1024)
# print(msg_back)
return msg_back.decode()
else:
return 'did not send anything'
class DeviceClient:
"""This class is meant to be inherited by classes dedicated to specific devices"""
def __init__(self, dev_id, host='localhost', port=get.port):
self.dev_id = dev_id
self.host = host
self.port = port
def query(self, msg):
return self.send(f"{self.dev_id}::Q::{msg}")
def write(self, msg):
self.send(f"{self.dev_id}::W::{msg}")
return "empty"
def read(self):
return self.send(f"{self.dev_id}::R")
def get_id(self):
return self.query('*IDN?')
def reset(self):
self.write('*RST')
return "reset"
def send(self, msg):
return send(msg, self.host, self.port)
if __name__ == "__main__":
response = send('test')
print(response)