【问题标题】:Display mod_wsgi error on webpage在网页上显示 mod_wsgi 错误
【发布时间】:2012-10-17 15:08:10
【问题描述】:

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

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

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

可能类似于 php 的 error_reporting 选项。

【问题讨论】:

    标签: mod-wsgi error-reporting


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      【解决方案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]
      

      【讨论】:

        猜你喜欢
        • 2020-06-23
        • 2015-07-23
        • 1970-01-01
        • 2021-11-24
        • 2018-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-11
        相关资源
        最近更新 更多