# server.py import asyncio import websockets async def echo(websocket, path): async for message in websocket: print(f"server Received: {message}") await websocket.send(f"Server response: {message}") start_server = websockets.serve(echo, "localhost", 6789) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever()
# client.py import asyncio import websockets async def hello(): uri = "ws://localhost:6789" async with websockets.connect(uri) as websocket: await websocket.send("Hello, world!") response = await websocket.recv() print(f"client Received: {response}") asyncio.get_event_loop().run_until_complete(hello())
标签:websocket,server,服务端,客户端,async,import,65websocket,websockets,asyncio From: https://www.cnblogs.com/chenzhi2023/p/18458959