开发者

BaseHTTPRequestHandler hangs when being run by pythonw.exe 3.1

开发者 https://www.devze.com 2023-02-13 00:17 出处:网络
The following code works fine with python.exe but fails with pythonw.exe. I\'m using Python 3.1 on Windows 7.

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.


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号