-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
44 lines (39 loc) · 1.51 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 sys
import json
import socket
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
TCP_server_ip = config['DEFAULT']['internet_ip']
TCP_server_port = int(config['DEFAULT']['TCP_server_port'])
class TCPClient:
def __init__(self, host=TCP_server_ip, port=TCP_server_port):
"""Create a TCP client that can send and receive messages from a persistent connection."""
self.host = host
self.port = port
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((host, port))
def send_request(self, request):
"""Send a JSON request to the server and return the JSON response."""
self.connection.sendall(json.dumps(request).encode())
return json.loads(self.connection.recv(1024).decode())
def close_connection(self):
"""Close the connection to the server."""
self.connection.close()
if __name__ == '__main__':
# Check if a command-line argument is provided
if len(sys.argv) == 2:
command = sys.argv[1]
client = TCPClient()
result = client.send_request({'command': command})
print(result)
client.close_connection()
elif len(sys.argv) == 3:
command = sys.argv[1]
value = sys.argv[2]
client = TCPClient()
result = client.send_request({'command': command, 'value': value})
print(result)
client.close_connection()
else:
print("Usage: python3 client.py 'setMAC' 'MAC_ADDRESS'")