为什么不是Flask给我一个交互式调试器

为什么不是Flask给我一个交互式调试器

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

问题描述

这里有一个简单的Flask应用程序,它有一个语法错误(应该是,那么你的方法可能会工作。 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 DebuggedApplicationapplication = 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给我一个交互式调试器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:27