| 1 |
import asyncio |
| 2 |
import logging |
| 3 |
|
| 4 |
from websockets.asyncio.server import serve |
| 5 |
from websockets.exceptions import WebSocketException |
| 6 |
|
| 7 |
|
| 8 |
logging.basicConfig(level=logging.WARNING) |
| 9 |
|
| 10 |
HOST, PORT = "0.0.0.0", 9002 |
| 11 |
|
| 12 |
|
| 13 |
async def echo(ws): |
| 14 |
try: |
| 15 |
async for msg in ws: |
| 16 |
await ws.send(msg) |
| 17 |
except WebSocketException: |
| 18 |
pass |
| 19 |
|
| 20 |
|
| 21 |
async def main(): |
| 22 |
async with serve( |
| 23 |
echo, |
| 24 |
HOST, |
| 25 |
PORT, |
| 26 |
server_header="websockets.asyncio", |
| 27 |
max_size=2**25, |
| 28 |
) as server: |
| 29 |
try: |
| 30 |
await server.serve_forever() |
| 31 |
except asyncio.CancelledError: |
| 32 |
pass |
| 33 |
|
| 34 |
|
| 35 |
if __name__ == "__main__": |
| 36 |
asyncio.run(main()) |