[Python][websockets] Websocket Echoサーバーを実装する

websocketsのドキュメントをそのまま動かしてみた。 https://websockets.readthedocs.io/en/stable/ 実装 #!/usr/bin/env python import asyncio import websockets async def echo(websocket, path): async for message in websocket: print(f"recv {len(message)}") await websocket.send(message) start_server = websockets.serve(echo, "localhost", 8765) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever() server.py import asyncio import websockets async def hello(): uri = "ws://localhost:8765" async with websockets.connect(uri) as websocket: await websocket.send("Hello world!") await websocket.recv() asyncio.get_event_loop().run_until_complete(hello())...

 · 1 min · しむどん三度無視