2

我已经设置了 mod_wsgi 来提供 python 文件。确实如此。

但是,如果我在 .py 文件上有语法错误(例如缩进错误),我会在页面加载时收到服务器错误,并且必须转到日志查看错误发生的原因(即缩进)

是否可以让 mod_wsgi 在页面上显示错误(至少对于开发环境)?

可能类似于 php 的 error_reporting 选项。

4

2 回答 2

3

不完全的。您可以获得的最接近的是:

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

这只适用于实际传播到 WSGI 服务器级别的请求执行期间发生的错误。

它不适用于加载 WSGI 脚本本身时出现的错误,或者在使用框架并且框架本身捕获错误并变成 500 页的情况下。在后者中,框架通常具有启用调试页面的方法。

现在,听起来您没有使用框架,而是在编写原始 WSGI 脚本。如果您是 Python Web 编程新手,则不建议您编写原始 WSGI。因此,请改用框架,它应该为您提供您想要的功能。

于 2012-10-27T22:45:45.163 回答
2

通过一些工作(非常感谢其他 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]
于 2017-12-31T06:47:21.550 回答