echo-server.py 481 B

123456789101112131415161718192021
  1. #!/usr/bin/env python
  2. # From https://github.com/aaugustin/websockets/blob/main/example/echo.py
  3. import asyncio
  4. import websockets
  5. import os
  6. LOCAL_WS_SERVER_PORT = os.environ.get('LOCAL_WS_SERVER_PORT', '8765')
  7. async def echo(websocket, path):
  8. async for message in websocket:
  9. await websocket.send(message)
  10. async def main():
  11. async with websockets.serve(echo, "localhost", LOCAL_WS_SERVER_PORT):
  12. await asyncio.Future() # run forever
  13. asyncio.run(main())