From 3dae0805204dc3ee758d7261cd26cfccb0b7d6aa Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Mon, 25 May 2026 19:27:57 +0200 Subject: [PATCH] Add client.py: test client for the TCP push server Connects to collect.py's push server and prints each incoming snapshot as pretty-printed JSON, separated by a dashed line. Co-Authored-By: Claude Sonnet 4.6 --- client.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 client.py diff --git a/client.py b/client.py new file mode 100644 index 0000000..909ef4b --- /dev/null +++ b/client.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +""" +WeConnect push-server test client. + +Connects to the collect.py TCP push server and prints each incoming +snapshot to stdout as pretty-printed JSON. + +Usage +----- + python client.py + python client.py --host 192.168.1.10 --port 9999 +""" + +import argparse +import json +import socket +import sys + + +def main() -> None: + p = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter, + ) + p.add_argument("--host", default="127.0.0.1", help="Server host (default: 127.0.0.1)") + p.add_argument("--port", type=int, default=9999, help="Server port (default: 9999)") + args = p.parse_args() + + print(f"Connecting to {args.host}:{args.port} …", flush=True) + try: + sock = socket.create_connection((args.host, args.port)) + except OSError as e: + print(f"Connection failed: {e}", file=sys.stderr) + sys.exit(1) + + print("Connected. Waiting for data (Ctrl-C to quit).\n", flush=True) + buf = "" + try: + with sock: + while True: + chunk = sock.recv(4096).decode() + if not chunk: + print("Server closed the connection.") + break + buf += chunk + while "\n" in buf: + line, buf = buf.split("\n", 1) + line = line.strip() + if not line: + continue + try: + data = json.loads(line) + print(json.dumps(data, indent=2)) + print("-" * 60, flush=True) + except json.JSONDecodeError as e: + print(f"[invalid JSON] {e}: {line!r}", file=sys.stderr) + except KeyboardInterrupt: + print("\nStopped by user.") + + +if __name__ == "__main__": + main() \ No newline at end of file