example/asyncio/echo.py
raw| 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """Echo server using the asyncio API.""" |
| 4 | |
| 5 | import asyncio |
| 6 | from websockets.asyncio.server import serve |
| 7 | |
| 8 | |
| 9 | async def echo(websocket): |
| 10 | async for message in websocket: |
| 11 | await websocket.send(message) |
| 12 | |
| 13 | |
| 14 | async def main(): |
| 15 | server = await serve(echo, "localhost", 8765) |
| 16 | await server.serve_forever() |
| 17 | |
| 18 | |
| 19 | if __name__ == "__main__": |
| 20 | asyncio.run(main()) |