Debian Codegraph @

testing/web-platform/tests/tools/third_party/websockets/example/quickstart/server_secure.py

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