How to Shut Down a Python Simplehttpserver

How do I shut down a python simpleHTTPserver?

You are simply sending signals to the processes. kill is a command to send those signals.

The keyboard command Ctrl+C sends a SIGINT, kill -9 sends a SIGKILL, and kill -15 sends a SIGTERM.

What signal do you want to send to your server to end it?

How do you stop a python SimpleHTTPServer in Terminal?

CTRL + C is usually the right way to kill the process and leave your terminal open.

How to shut down a Python TCPServer or HTTPServer or SimpleHTTPServer?

Yes, the BaseServer.shutdown() method is thread-safe.

The serve_forever method serves until the shutdown flag is set, and it is shutdown()s task to set that flag. The method states, in the docstring:

Blocks until the loop has finished. This must be called while
serve_forever() is running in another thread, or it will deadlock.

Note that this has nothing to do with the GIL; that only limits how many cores a Python process can effectively make use of outside of C extensions and I/O.

Python's SimpleHTTPServer started in a thread won't close the port

As @LFJ say, this is probably due to the allow_reuse_address attribute of the TCPServer.

httpd = SocketServer.TCPServer(("", PORT), Handler, bind_and_activate=False)
httpd.allow_reuse_address = True

try:
httpd.server_bind()
httpd.server_activate()
except:
httpd.server_close()
raise

Equivalent code :

SocketServer.TCPServer.allow_reuse_address = True
https = SocketServer.TCPServer(("", PORT), Handler)

Let's explain a bit why.

When you enable TCPServer.allow_reuse_address, it adds an option on the socket :

class TCPServer:
[...]
def server_bind(self):
if self.allow_reuse_address:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
[...]

What is socket.SO_REUSEADDR ?

This socket option tells the kernel that even if this port is busy (in
the TIME_WAIT state), go ahead and reuse it anyway. If it is busy,
but with another state, you will still get an address already in use
error. It is useful if your server has been shut down, and then
restarted right away while sockets are still active on its port. You
should be aware that if any unexpected data comes in, it may confuse
your server, but while this is possible, it is not likely.

In fact, it allows the reuse of your socket socket binding address. If another process try to bind while the socket is not listening, the process will be allowed to use this socket binding address.

The reason you need to enable that is because you don't shutdown properly your TCPServer. In order to close it properly, you must run shutdown method, which will close the thread launched by server_forever and then close the socket properly by calling the server_close method.

def shutdown():
global httpd
global please_die
print "Shutting down"

try:
please_die.wait() # how do you do?
httpd.shutdown() # Stop the serve_forever
httpd.server_close() # Close also the socket.
except Exception:
traceback.print_exc(file=sys.stdout)

How can I exit SimpleHTTPServer without closing terminal?

^C will close it (control + c)

Example of me testing control + c to send an interupt and exit:

PS C:\> python -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 ...

Keyboard interrupt received, exiting.

Note, I am using http.server as this replaces SimpleHTTPServer in Python 3 but it should work the same for you.



Related Topics



Leave a reply



Submit