2011-02-24 77 views
1

以下代碼可與python.exe協同工作,但pythonw.exe失敗。我在Windows 7上使用Python 3.1。BaseHTTPRequestHandler在由pythonw.exe運行時掛起3.1

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

當我開始發送響應時出錯。沒有人寫回來。如果我嘗試另一個http連接,它將不會連接。我也嘗試使用self.wfile,但沒有運氣。

回答

1

您正在打印到標準輸出。 pythonw.exe doens't沒有標準輸出,因爲它沒有連接到終端。我的猜測是這與它有關。

嘗試將stdout重定向到文件,或更快,刪除print()

+0

謝謝。我重定向了sys.stdout和sys.stderr以打開(os.devnull,'w')並且它運行平穩。 – llc 2011-02-25 13:30:44