I am running my HTTPServer
in a separate thread (using the threading module which has no way to stop threads...) and want to stop serving requests when the main thread also shuts down.
The Python documentation states that BaseHTTPServer.HTTPServer
is a subclass of SocketServer.TCPServer
, which supports a shutdown
method, but it is missing in HTTPServer
.
The whole BaseHTTPServer
module has very little documentation :(
The event-loops ends on SIGTERM, Ctrl+C or when
shutdown()
is called.server_close()
must be called afterserver_forever()
to close the listening socket.Simple server stoppable with user action (SIGTERM, Ctrl+C, ...):
Server running in a thread:
I tried all above possible solution and ended up with having a "sometime" issue - somehow it did not really do it - so I ended up making a dirty solution that worked all the time for me:
If all above fails, then brute force kill your thread using something like this: