Add tcp server

This commit is contained in:
2025-04-19 02:35:03 +02:00
parent bbf039a596
commit 5121c2a27b
5 changed files with 409 additions and 139 deletions

View File

@@ -1,15 +1,48 @@
import socket
import sys
import logging
from time import sleep
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG,
stream=sys.stdout)
sock.connect(("localhost", 1234))
sock.sendall(b"asdf")
data = sock.recv(1024)
print(data.decode())
host = "localhost"
port = 65432
while True:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
client_socket.send(b"""{
"jsonrpc": "2.0",
"id": "1",
"method": "SetLedPattern",
"params": {
"led": 19,
"pattern": 3.1
}
}
""")
try:
res = client_socket.recv(1024)
if not res:
logging.info("Client disconnected")
break
logging.info(f"SetLedPattern response: {res.decode()}")
except socket.timeout:
logging.error("Connection timed out")
break
client_socket.close()
sleep(1)
if __name__ == "__main__":
main()