Skip to content

Commit

Permalink
Handle SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
jpflouret committed Mar 19, 2024
1 parent 7386da4 commit 5e6199c
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import signal
import sys
import threading
import time

bindAddress = ""
serverPort = 8080
Expand All @@ -14,12 +18,20 @@ def do_GET(self):

if __name__ == "__main__":
webServer = HTTPServer((bindAddress, serverPort), MyServer)
print("Server listening on %s:%s" % (bindAddress, serverPort))

try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
def stop(signal_number, frame):
print('Server shutting down.')
webServer.shutdown()
sys.exit(0)

signal.signal(signal.SIGTERM, stop)
signal.signal(signal.SIGINT, stop)

thread = threading.Thread(target=webServer.serve_forever)
thread.daemon = True
thread.start()

print("Server listening on %s:%s" % (bindAddress, serverPort))

webServer.server_close()
print("Server stopped.")
while True:
time.sleep(1)

0 comments on commit 5e6199c

Please sign in to comment.