3

I was using looking at the docs of http.server and ran the code:

def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

Now I can't close the server running on port 8000 , How do I close it?

Yashik
  • 391
  • 5
  • 17
  • Possible duplicate of [How to shut down python server](https://stackoverflow.com/questions/42763311/how-to-shut-down-python-server) – Raj Sappidi Aug 18 '18 at 15:42

2 Answers2

4

You can use ^C (control+c) to shut down python server

or use httpd.shutdown() in the code to close it.

See the detailed answer at

https://stackoverflow.com/a/42763796/10241547

nick
  • 1,090
  • 1
  • 11
  • 24
Raj Sappidi
  • 156
  • 8
2

The HTTPServer is a subclass of TCPServer class

When serve_forever() function is executed it periodically checks for the value of __shutdown_request variable. If its value becomes True, the serve_forever() function exits its main loop.

The shutdown() method sets that variable to True thus initiating loop break.

leotrubach
  • 1,509
  • 12
  • 15