1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import asyncio from websockets.server import serve from websockets.legacy.protocol import WebSocketCommonProtocol from websockets.exceptions import ConnectionClosed async def echo(websocket: WebSocketCommonProtocol): while True: try: await websocket.send('Hello, world') except ConnectionClosed: break await asyncio.sleep(1.0) async def main(): async with serve(echo, "localhost", 8765): await asyncio.Future() asyncio.run(main())
You can clone a snippet to your computer for local editing. Learn more.