2012-10-27 51 views
2

我已經設置了mod_wsgi來提供python文件。確實如此。但是,如果我在.py文件中有語法錯誤(例如縮進錯誤),那麼當頁面加載時,我會收到服務器錯誤,並且必須轉到日誌以查看錯誤發生的原因(即縮進)在網頁上顯示mod_wsgi錯誤

是否有可能讓mod_wsgi在頁面上顯示錯誤(至少對於dev環境)?

可能類似於php的error_reporting選項。

回答

3

不完全。你可以得到最接近的是:

http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Error_Catching_Middleware

這將僅適用於實際傳播到WSGI服務器級別的請求的執行過程中發生的錯誤做。

對於加載WSGI sript本身的錯誤,或者在使用框架並且框架本身捕獲錯誤並將其轉換爲500頁的情況下,它將不起作用。在後者中,框架通常具有啓用調試頁面的手段。

現在,這聽起來像你沒有使用框架,但正在編寫原始的WSGI腳本。如果您不熟悉Python Web編程,則不建議使用原始WSGI。因此,請使用框架,它應該爲您提供您想要的功能。

1

通過一些工作(很多感謝其他Stack Overflow貢獻者!),您可以獲得良好的錯誤信息給瀏覽器。當然,您的WSGI「根」位於「應用程序」定義中:

def application(environ, start_response): 
    import linecache, sys 
    try: 
     from cgi import parse_qs, escape  # Application starts here 
     start_response('200 OK', [('Content-type', 'text/html'),]) #to here 
     return ["Whatever application wants to say."] 

    except:         # Error output starts here 
     exc_type, exc_obj, tb = sys.exc_info() 
     f = tb.tb_frame 
     lineno = tb.tb_lineno 
     filename = f.f_code.co_filename 
     linecache.checkcache(filename) 
     line = linecache.getline(filename, lineno, f.f_globals) 
     es = '''Error in {}, Line {} "{}": {}'''.format(filename, lineno, line.strip(), exc_obj) 
     start_response('200 OK', [('Content-type', 'text/html'),]) 
     return [es]