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
Stored Procedures With Sqlalchemy
Pyodbc Error Data Source Name Not Found and No Default Driver Specified Paradox
How to Merge Two Cnn That Are Trained Over Different Data Stream
How to Display a Plot in Fullscreen
Background Color for Tk in Python
How to Convert a Pandas Dataframe to a Pytorch Tensor
How to Get Python to Detect for No Input
Splitting Strings into Numbers (Python)
Python: Read Several Json Files from a Folder
How to Read a Column Without Header from CSV and Save the Output in a Txt File Using Python
How to Convert a Datetime Object to Milliseconds Since Epoch (Unix Time) in Python
Find Matching Rows in 2 Dimensional Numpy Array
Replace a Word in a String by Indexing Without "String Replace Function" -Python
Asking the User for Input Until They Give a Valid Response
Convenient Way to Handle Deeply Nested Dictionary in Python
How to Center a Window on the Screen in Tkinter
Set Working Directory in Python/Spyder So That It's Reproducible