1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] in python threadinghttpserver - stop server from inside handler

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following "main" code:

    with http.server.ThreadingHTTPServer( ("", port), Handler ) as daemon:
    print(f"serving on port {port} process {os.getpid()} ")
    while True:
    try:
    daemon.handle_request()
    except KeyboardInterrupt:
    print("\nexiting")
    return 0


    which works great. However - this is only used for local testing and on the ci machine - so I want an easy way to shut it down and replace it with the next one - so inside the Handler do_GET(self) I added the following code:

    if path == "/shutdown":
    throw Exception("shut down")


    and if I do a curl http://localhost:9000/shutdown.... I do indeed see an exception - nicely displayed and swallowed inside the thread.

    And I've found it remarkably hard to figure out any way of stopping the server from inside the handler - because the handler is running in another process.

    Is there a simple way of doing it?

    Continue reading...

Compartilhe esta Página