Run Python HTTPServer in Background and Continue Script Execution

11,795

You can start the server in a different thread: https://docs.python.org/2/library/thread.html

So something like:

def start_server():
    # Setup stuff here...
    server.serve_forever()

# start the server in a background thread
thread.start_new_thread(start_server)

print('The server is running but my script is still executing!')
Share:
11,795
Michael Scott
Author by

Michael Scott

Updated on July 04, 2022

Comments

  • Michael Scott
    Michael Scott almost 2 years

    I am trying to figure out how to run my overloaded customized BaseHTTPServer instance in the background after running the "".serve_forever() method.

    Normally when you run the method execution will hang until you execute a keyboard interrupt, but I would like it to serve requests in the background while continuing script execution. Please help!