为什么不是Flask给我一个交互式调试器? [英] Why isn't Flask giving me an interactive debugger?

查看:226
本文介绍了为什么不是Flask给我一个交互式调试器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个简单的Flask应用程序,它有一个语法错误(应该是 sys.version ,而不是 sys.version()导入sys



 应用程序= Flask(__ name__)
app.config.from_object(__ name__)
$ b $ app_route('/')
def index():
version = sys.version()
return这是索引页面。Python version info:{}。format(version)
$ b $ if if __name__ =='__main__':
app .run(debug = True)

当我在本地运行并尝试访问



我尝试了各种各样的方法来完成这个工作:


  • set app.debug = True app = Flask(__ name __)

  • 之后,基于
    $ / $ b $ / $> $ / $> $ / $> $ / $> $ / $> $ / $> b

    from werkzeug.debug import DebuggedApplication
    application = DebuggedApplication(app,True)



    但似乎没有帮助。

    我知道调试器不能在分支环境中工作,但根据这个链接, mod_wsgi的默认模式是不是。我的Apache配置是:

    pre $ WSGIDaemonProcess flasktest user = david group = devgrp threads = 1 python-path = / apps / www / 80 /wsgi-scripts/FlaskTest:/apps/.virtualenvs/flasktest/lib/python3.4/site-packages



    所以为什么我不能得到漂亮的调试器?编辑:这是修改的文件与我的补充(不是工作)

      from flask import Flask 
    import sys
    $ b $ app = Flask(__ name__)
    app.config.from_object(__ name__)
    app.debug = True

    $ from werkzeug.debug import DebuggedApplication
    application = DebuggedApplication(app,True)

    @ app.route('/')
    def index():
    version = sys.version()
    return这是索引页面。 {}。格式(版本)
    $ b $如果__name__ =='__main__':
    app.run(debug = True)


    解决方案

    添加WSGI middlew对一个Flask应用程序,包装并替换应用程序的 wsgi_app 属性。在调试中间件的情况下,设置 app.debug = True 以便Flask不会将异常转换为500个响应。

      app.debug = True 
    app.wsgi_app = DebuggedApplication(app.wsgi_app,evalex = True)






    在公用服务器上运行调试器会带来安全风险。尽管最近在运行代码之前需要更改PIN码,但调试器仍然可能在未知的情况下变得不安全。通常情况下,通过使用日志记录可以获取有关生产中的错这个答案是如何设置的基本总结。



    如果你已经替换了 app 而不是调用它 application ,那么你的方法可能会工作。 code>。 mod_wsgi可能在 application 之前选择 app ,所以它仍然在运行原始的应用程序。尽管如此,仍然推荐使用 app.wsgi_app


    Here's a simple Flask app with a syntax error (it should be sys.version, not sys.version()

    from flask import Flask, url_for, request, render_template
    import sys
    
    app = Flask(__name__)
    app.config.from_object(__name__)
    
    @app.route('/')
    def index():
        version = sys.version()
        return "This is the index page.  Python version info: {}".format(version)
    
    if __name__ == '__main__':
        app.run(debug=True)
    

    When I run it locally and try to access http://localhost:5000, I get the nice Werkzeug debugger:

    but when I run it on a remote server (Apache 2.4.17, mod_wsgi 4.4.21), I only get a generic 500 error page:

    I've tried various things to make this work:

    • set app.debug = True right after app = Flask(__name__)
    • based on this link, I tried adding this code as well:

    from werkzeug.debug import DebuggedApplication application = DebuggedApplication(app, True)

    but nothing seems to help.

    I know that the debugger doesn't work in forked environments, but according to this link, the default mode for mod_wsgi is to not be forked. My Apache config is:

    WSGIDaemonProcess flasktest user=david group=devgrp threads=1 python-path=/apps/www/80/wsgi-scripts/FlaskTest:/apps/.virtualenvs/flasktest/lib/python3.4/site-packages
    

    So why can't I get the pretty debugger?

    EDIT: Here's the modified file with my additions (that aren't working)

    from flask import Flask
    import sys
    
    app = Flask(__name__)
    app.config.from_object(__name__)
    app.debug = True
    
    from werkzeug.debug import DebuggedApplication
    application = DebuggedApplication(app, True)  
    
    @app.route('/')
    def index():   
        version = sys.version()
        return "This is the index page.  Python version info: {}".format(version)
    
    if __name__ == '__main__':
        app.run(debug=True)   
    

    解决方案

    To add WSGI middleware to a Flask app, wrap and replace the app's wsgi_app attribute. In the case of the debug middleware, set app.debug = True as well so that Flask doesn't convert exceptions to 500 responses.

    app.debug = True
    app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True)
    


    It is a security risk to run the debugger on a public server. Despite the recent change requiring a PIN before running code, the debugger may still be insecure in unforseen ways. Typically, you get information about errors in production by using logging. This answer is a good basic summary of how to set that up.


    Your method would probably work if you had replaced app rather than calling it application. mod_wsgi was probably picking up app before application, so it was still running the original app. It's still recommended to wrap app.wsgi_app though.

    这篇关于为什么不是Flask给我一个交互式调试器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆