in python threadinghttpserver - stop server from inside handler

193 views Asked by At

I have the following "main" code:

with  http.server.ThreadingHTTPServer( ("", port), Handler )  as daemon:
    print(f"serving on port {port} process {os.getpid()} ")
    while True:
        try:
            daemon.handle_request()
        except KeyboardInterrupt:
            print("\nexiting")
            return 0

which works great. However - this is only used for local testing and on the ci machine - so I want an easy way to shut it down and replace it with the next one - so inside the Handler do_GET(self) I added the following code:

   if path == "/shutdown":
       throw Exception("shut down")

and if I do a curl http://localhost:9000/shutdown.... I do indeed see an exception - nicely displayed and swallowed inside the thread.

And I've found it remarkably hard to figure out any way of stopping the server from inside the handler - because the handler is running in another process.

Is there a simple way of doing it?

0

There are 0 answers