wsproxytest/server.py

28 lines
606 B
Python
Executable File

#!/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())