Search code examples
pythonhttpserverpythonw

Execute a script without a console window without using pythonw.exe


I have a python script that uses the http.server module that I would like to run without a terminal window being shown. Unfortunately, due to how I'm doing this, running the script in pythonw.exe does not work.

Here is the script:

import os
from http.server import CGIHTTPRequestHandler, HTTPServer
handler = CGIHTTPRequestHandler
handler.cgi_directories = ['/scripts']
server = HTTPServer(('localhost', 1271), handler)
server.serve_forever()

Unfortunately, I don't know any way get any error logs because, y'know, pythonw doesn't show the console. If anyone can tell me how to get the error logs, I'll be happy to add them to the bottom of this post.

I'm running 64-bit Windows 10 and python 3.6.6, if that makes a difference.

I'm sorry if this is a stupid question, but I—for the life of me—cannot find a solution anywhere.


Solution

  • You can just store output to a text file like this:

    from http.server import CGIHTTPRequestHandler, HTTPServer
    import sys
    
    handler = CGIHTTPRequestHandler
    handler.cgi_directories = ['/scripts']
    server = HTTPServer(('localhost', 1271), handler)
    sys.stderr = open('log.txt', 'w', 1)
    server.serve_forever()