2013-07-03 62 views
3

我想通過IIS運行Flask作爲一個簡單的CGI應用程序。什麼時候sys.stdin在Python中沒有?

我有以下代碼:

from wsgiref.handlers import CGIHandler 
from flask import Flask 
app = Flask(__name__) 

@app.route('/') 
def main(): 
    return 'Woo woo!' 

CGIHandler().run(app) 

我在Windows乳寧的Python 3.3。我得到以下錯誤:

File "C:\Python33\lib\wsgiref\handlers.py", 
line 509, in __init__(self, sys.stdin.buffer, sys.stdout.buffer, sys.stderr,) 
AttributeError: 'NoneType' object has no attribute 'buffer' ". 

我添加了一些日誌代碼,並且事實證明,sys.stdinNone

Python是添加到IIS的CGI處理程序如下:

Request path: *.py 
Executable: C:\Windows\py.exe -3 %s %s 

那麼,爲什麼sys.stdin無,我怎麼能解決這個問題?

編輯

看起來sys.stdin是沒有,因爲file descriptor is invalid

回答

3

有趣。你已經回答了你自己的一半問題。另一半(「我該如何解決它」)很容易,只需打開一些適合的東西(os.devnull是顯而易見的)並將sys.stdin設置爲指向那裏。你需要做的sys.stdout的和sys.stderr爲好,大概,所以像這樣:

import os, sys 
for _name in ('stdin', 'stdout', 'stderr'): 
    if getattr(sys, _name) is None: 
     setattr(sys, _name, open(os.devnull, 'r' if _name == 'stdin' else 'w')) 
del _name # clean up this module's name space a little (optional) 
from wsgiref.handlers ... 

應該做的伎倆。

+0

這最終成爲我的解決方案的一半 - 另一半使用'IISCGIHandler()',因爲顯然IIS有一些問題。另外,我剛剛用'os.devnull'取代了'stdin',並且生活很美好。 –

相關問題