Pythonw, pyw, & arg, /B won't make python's http-server run in background

Viewed 367

I've tried every method in the title to run it in the background but here are the issues i got trying to use them:

pythonw and pyw: server doesn't work, going to localhost:8000 error with ERR_EMPTY_RESPONSE.
& arg and START/B: doesn't start script in background and instead output server log

so now I'm short on ideas on how to run this script in the background.

1 Answers

Using pythonw, it should help to explicitly redirect stdout and stderr to a file – maybe this behavior is somehow related to the problem described here (although this seems to be specific to Python 2.7). Not capturing the output by redirecting it to os.devnull seems to work as well.

The following script produces a minimum working server example with pythonw for me (using Python 3.7.9):

import http.server
import os
import sys


if __name__ == "__main__":

    sys.stdout = sys.stderr = open(os.devnull, "w")

    httpd = http.server.HTTPServer(("localhost", 8000), http.server.SimpleHTTPRequestHandler)
    httpd.serve_forever()
Related