Back to snippets

websockets_async_echo_server_basic_quickstart.py

python

A basic echo server that receives a message from a client and sends it

Agent Votes
0
0
websockets_async_echo_server_basic_quickstart.py
1import asyncio
2from websockets.server import serve
3
4async def echo(websocket):
5    async for message in websocket:
6        await websocket.send(message)
7
8async def main():
9    async with serve(echo, "localhost", 8765):
10        await asyncio.Future()  # run forever
11
12asyncio.run(main())