Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shut down python server

Tags:

python

server

Used this code to run a python server:

import os
from http.server import SimpleHTTPRequestHandler, HTTPServer                                                                                                                                   

os.chdir('c:/users/owner/desktop/tom/tomsEnyo2.5-May27')                                                                                                                                                                                      
server_address = ('', 8000)                                                                                                                                                                    
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)                                                                                                                                   
httpd.serve_forever()

How to make it stop?

like image 465
Tom Bar-Gal Avatar asked Mar 13 '17 12:03

Tom Bar-Gal


People also ask

What is Python SimpleHTTPServer?

The SimpleHTTPServer module is a Python module that enables a developer to lay the foundation for developing a web server. However, as sysadmins, we can use the module to serve files from a directory. Usage. Python must be installed to use the SimpleHTTPServer module.


1 Answers

Your question is ambiguous - if your running the server via shell i.e. python myscript.py, simply press crtl + C.

If you want to close it elegantly using code, you must decide on some condition, or point, or exception to call it shutdown. You can add a block and call httpd.shutdown() - as HttpServer itself is a SocketServer.TCPSServer subclass:

The first class, HTTPServer, is a SocketServer.TCPServer subclass, and therefore implements the SocketServer.BaseServer interface. It creates and listens at the HTTP socket, dispatching the requests to a handler.

So the BaseServer has a method shutdown(), hence being a subclass HttpServer has it too.

for example:

import os
from http.server import SimpleHTTPRequestHandler, HTTPServer                                                                                                                                   

os.chdir('c:/users/owner/desktop/tom/tomsEnyo2.5-May27')                                                                                                                                                                                      
server_address = ('', 8000)   
try:
    httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)                                                                                                                                   
    httpd.serve_forever()
except Exception:
    httpd.shutdown()

Helpful relevant question -

  • How do I shutdown an HTTPServer from inside a request handler in Python?
  • How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?
like image 186
Nabeel Ahmed Avatar answered Oct 19 '22 05:10

Nabeel Ahmed