| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Server example with HTTP Basic Authentication over TLS |
| 4 |
|
| 5 |
import asyncio |
| 6 |
|
| 7 |
from websockets.legacy.auth import basic_auth_protocol_factory |
| 8 |
from websockets.legacy.server import serve |
| 9 |
|
| 10 |
async def hello(websocket): |
| 11 |
greeting = f"Hello {websocket.username}!" |
| 12 |
await websocket.send(greeting) |
| 13 |
|
| 14 |
async def main(): |
| 15 |
async with serve( |
| 16 |
hello, "localhost", 8765, |
| 17 |
create_protocol=basic_auth_protocol_factory( |
| 18 |
realm="example", credentials=("mary", "p@ssw0rd") |
| 19 |
), |
| 20 |
): |
| 21 |
await asyncio.get_running_loop().create_future() # run forever |
| 22 |
|
| 23 |
asyncio.run(main()) |