| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import pathlib |
| 5 |
import ssl |
| 6 |
|
| 7 |
from websockets.asyncio.server import serve |
| 8 |
|
| 9 |
async def hello(websocket): |
| 10 |
name = await websocket.recv() |
| 11 |
print(f"<<< {name}") |
| 12 |
|
| 13 |
greeting = f"Hello {name}!" |
| 14 |
|
| 15 |
await websocket.send(greeting) |
| 16 |
print(f">>> {greeting}") |
| 17 |
|
| 18 |
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) |
| 19 |
localhost_pem = pathlib.Path(__file__).with_name("localhost.pem") |
| 20 |
ssl_context.load_cert_chain(localhost_pem) |
| 21 |
|
| 22 |
async def main(): |
| 23 |
server = await serve(hello, "localhost", 8765, ssl=ssl_context) |
| 24 |
await server.serve_forever() |
| 25 |
|
| 26 |
if __name__ == "__main__": |
| 27 |
asyncio.run(main()) |