websocket_server.py 377 B

123456789101112131415161718
  1. #!/usr/bin/env python3
  2. # pip3 install websockets
  3. import asyncio
  4. import websockets
  5. async def echo(websocket):
  6. async for message in websocket:
  7. print(message)
  8. await websocket.send(message)
  9. async def serve(port):
  10. async with websockets.serve(echo, "0.0.0.0", port):
  11. await asyncio.Future()
  12. if __name__ == "__main__":
  13. asyncio.run(serve(9999))