fix: lack of SIGTERM handling.

This commit is contained in:
2026-05-09 20:05:34 +02:00
parent 286fcdbe5b
commit 2699efc773
4 changed files with 33 additions and 17 deletions
+16 -8
View File
@@ -15,19 +15,20 @@ Behavior:
for that request only. Values are case-insensitive and must be one of basic,nice,bootstrap.
Usage:
python3 ok_server.py # binds 0.0.0.0:8000, nice look
python3 ok_server.py # binds 0.0.0.0:8080, nice look
python3 ok_server.py --look basic
python3 ok_server.py -b 127.0.0.1 -p 8080 --look bootstrap
Test:
curl -i http://localhost:8000/ # text/plain for curl
curl -i "http://localhost:8000/?look=basic" # request-level override
curl -I http://localhost:8000/ # HEAD (headers only)
wget -S -O - http://localhost:8000/ # text/plain for wget
open http://localhost:8000/ # browser gets HTML variant per look
curl -i http://localhost:8080/ # text/plain for curl
curl -i "http://localhost:8080/?look=basic" # request-level override
curl -I http://localhost:8080/ # HEAD (headers only)
wget -S -O - http://localhost:8080/ # text/plain for wget
open http://localhost:8080/ # browser gets HTML variant per look
"""
import argparse
import signal
import sys
from html import escape
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
@@ -254,6 +255,13 @@ def run(bind: str, port: int, look: str):
addr = (bind, port)
try:
with ThreadingHTTPServer(addr, OkHandler) as httpd:
def _handle_termination(signum, _frame):
_ = signum
raise KeyboardInterrupt
signal.signal(signal.SIGINT, _handle_termination)
signal.signal(signal.SIGTERM, _handle_termination)
# attach chosen look to server so handlers can use it as default
httpd.look = look
print(f"Serving on {bind}:{port} (default look={look}) (Ctrl-C to stop)")
@@ -269,8 +277,8 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(description="HTTP OK test server")
parser.add_argument("-b", "--bind", default="0.0.0.0",
help="Address to bind to (default: 0.0.0.0)")
parser.add_argument("-p", "--port", type=int, default=8000,
help="Port to listen on (default: 8000)")
parser.add_argument("-p", "--port", type=int, default=8080,
help="Port to listen on (default: 8080)")
parser.add_argument("--look", choices=VALID_LOOKS, default=VALID_LOOKS[1],
help="Default HTML look for non-cli agents (basic, nice, bootstrap). Default: nice")
args = parser.parse_args()