modbus_test/test_socket_server.py

28 lines
944 B
Python

import socket
import json
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
if data:
req = json.loads(data)
res = {"queryData": ["ok"]}
if "queryAddr" in req.keys() and "axis-0" in req["queryAddr"]:
res["queryData"] = [10, 11, 12, 13, 14, 15]
if "queryAddr" in req and "world-0" in req["queryAddr"]:
res["queryData"] = [100, 101, 102, 103, 104, 105]
if req["reqType"] == "command":
res["cmdReply"] = ['ok']
conn.sendall(json.dumps(res).encode())