From 6e47f72ce8ccaa2448ce7bca592f71f63e9c86c1 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Wed, 18 Jun 2025 16:48:51 -0600 Subject: [PATCH] Add Python WebSocket client that prints headers --- README.md | 5 +++++ server.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100755 server.py diff --git a/README.md b/README.md index 0085360..699ea3a 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,11 @@ In another terminal, run the WebSocket server (listens on 8078): bun server.js ``` +Alternative server with Python: +```sh +uv run server.py +``` + Then, open your browser and navigate to `http://localhost:5173` and open the developer console to see how it doesn't work. In development console you can directly connect to the WebSocket server using: diff --git a/server.py b/server.py new file mode 100755 index 0000000..b6fc601 --- /dev/null +++ b/server.py @@ -0,0 +1,27 @@ +#!/usr/bin/env uv run +# /// script +# requires-python = ">=3.13" +# dependencies = [ +# "websockets", +# ] +# /// +import asyncio +import websockets +from websockets.http import Headers + +async def handler(ws): + while True: + msg = await ws.recv() + print(msg) + await ws.send(f"echo {msg}") + +async def headers_printer(path, req): + for hdr, value in req.headers.items(): + hdr += ":" + print(f"{hdr:30}{value}") + +async def main(): + async with websockets.serve(handler, '0.0.0.0', 8078, process_request=headers_printer): + await asyncio.Future() + +asyncio.run(main())