59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
import socket
|
|
import json
|
|
|
|
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
|
|
PORT = 9760 # Port to listen on (non-privileged ports are > 1023)
|
|
|
|
|
|
def handle_client(conn, addr):
|
|
print(f"Connected by {addr}")
|
|
try:
|
|
while True:
|
|
data = conn.recv(1024)
|
|
if not data:
|
|
break
|
|
req = json.loads(data)
|
|
res = {"queryData": ["ok"]}
|
|
|
|
if "queryAddr" in req.keys() and "axis-0" in req["queryAddr"]:
|
|
res["queryData"] = [
|
|
-8.487,
|
|
-8.681,
|
|
33.058,
|
|
88.070,
|
|
-75.010,
|
|
-10.566,
|
|
]
|
|
|
|
if "queryAddr" in req and "world-0" in req["queryAddr"]:
|
|
res["queryData"] = [
|
|
1282.244,
|
|
-75.427,
|
|
1772.476,
|
|
84.629,
|
|
34.519,
|
|
153.999,
|
|
]
|
|
|
|
if req["reqType"] == "command":
|
|
res["cmdReply"] = ["ok"]
|
|
conn.sendall(json.dumps(res).encode())
|
|
except json.JSONDecodeError as e:
|
|
print(f"JSON decode error: {e}")
|
|
except socket.error as e:
|
|
print(f"Socket error: {e}")
|
|
except Exception as e:
|
|
print(f"Unexpected error: {e}")
|
|
finally:
|
|
conn.close()
|
|
print(f"Connection with {addr} closed")
|
|
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
s.bind((HOST, PORT))
|
|
s.listen()
|
|
print(f"Server listening on {HOST}:{PORT}")
|
|
while True:
|
|
conn, addr = s.accept()
|
|
handle_client(conn, addr)
|