BaseHTTPRequestHandler hangs when being run by pythonw.exe 3.1

1k Views Asked by At

The following code works fine with python.exe but fails with pythonw.exe. I'm using Python 3.1 on Windows 7.

from http.server import BaseHTTPRequestHandler, HTTPServer

class FooHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        length = int(self.headers['Content-Length'])
        data = self.rfile.read(length)
        print(data)
        self.send_response(200)
        self.send_header('Content-Length', '0')
        self.end_headers()

httpd = HTTPServer(('localhost', 8000), FooHandler)
httpd.serve_forever()

Something wrong when I start sending responses. Nothing got written back. And if I try another http connection it won't connect. I also tried using self.wfile but no luck either.

1

There are 1 best solutions below

1
On BEST ANSWER

You are printing to stdout. pythonw.exe doens't have a stdout, as it's not connected to a terminal. My guess is that this has something to do with it.

Try to redirect stdout to a file, or quicker, remove the print().