summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/tools/third_party/websockets/example/faq/shutdown_server.py
diff options
context:
space:
mode:
Diffstat (limited to 'testing/web-platform/tests/tools/third_party/websockets/example/faq/shutdown_server.py')
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/faq/shutdown_server.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/faq/shutdown_server.py b/testing/web-platform/tests/tools/third_party/websockets/example/faq/shutdown_server.py
new file mode 100644
index 0000000000..1bcc9c90ba
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/faq/shutdown_server.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+
+import asyncio
+import signal
+import websockets
+
+async def echo(websocket):
+ async for message in websocket:
+ await websocket.send(message)
+
+async def server():
+ # Set the stop condition when receiving SIGTERM.
+ loop = asyncio.get_running_loop()
+ stop = loop.create_future()
+ loop.add_signal_handler(signal.SIGTERM, stop.set_result, None)
+
+ async with websockets.serve(echo, "localhost", 8765):
+ await stop
+
+asyncio.run(server())