-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
30 lines (22 loc) · 891 Bytes
/
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
import socket
import json
class Client:
def __init__(self, ip_address: str, port: int):
self.ip_address = ip_address
self.port = port
# Connect to server
self.s = socket.socket()
self.s.settimeout(5000)
self.s.connect(socket.getaddrinfo(ip_address, port)[0][-1])
# Send a function call to the server
def execute(self, function: str, parameters: dict = {}):
payload = {"function": function, "parameters": parameters}
# Send function with parameters
print("Sending:", payload)
self.s.send(bytes(json.dumps(payload), "utf-8"))
# Receive response
response = json.loads(self.s.recv(8192).decode("utf-8"))
print("Response:", response)
if not response["success"]:
raise Exception("Server returned error: " + response["error"])
return response