| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# WS server example listening on a Unix socket |
| 4 |
|
| 5 |
import asyncio |
| 6 |
import os.path |
| 7 |
|
| 8 |
from websockets.legacy.server import unix_serve |
| 9 |
|
| 10 |
async def hello(websocket): |
| 11 |
name = await websocket.recv() |
| 12 |
print(f"<<< {name}") |
| 13 |
|
| 14 |
greeting = f"Hello {name}!" |
| 15 |
|
| 16 |
await websocket.send(greeting) |
| 17 |
print(f">>> {greeting}") |
| 18 |
|
| 19 |
async def main(): |
| 20 |
socket_path = os.path.join(os.path.dirname(__file__), "socket") |
| 21 |
async with unix_serve(hello, socket_path): |
| 22 |
await asyncio.get_running_loop().create_future() # run forever |
| 23 |
|
| 24 |
asyncio.run(main()) |