import socket import json import struct HOST = "127.0.0.1" # The server's hostname or IP address PORT = 6379 # The port used by the server def reply(s): print(f"Received {data!r}") reply = json.dumps({ "test": data.decode("utf-8") }).encode('utf-8') s.sendall(struct.pack(">I", len(reply)) + reply) print("Started program") with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) print("Connected") while True: data = s.recv(1024) if data: reply(s) break