pip install websockets
python
import asyncio
import websockets
async def handle_connection(websocket, path):
async for message in websocket:
print("Received message:", message)
await websocket.send("Echo: " + message)
start_server = websockets.serve(handle_connection, "localhost", 8888)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
python
import asyncio
import websockets
async def send_message():
async with websockets.connect("ws://localhost:8888") as websocket:
await websocket.send("Hello, Server!")
response = await websocket.recv()
print("Received response:", response)
asyncio.get_event_loop().run_until_complete(send_message())