在0.0.0.0上运行Flask应用程序时为什么会得到404 [英] Why do I get 404 when running Flask app on 0.0.0.0

查看:603
本文介绍了在0.0.0.0上运行Flask应用程序时为什么会得到404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在AWS EC2实例(Ubuntu)上测试Flask应用程序。

I am testing my Flask app on AWS EC2 instance (Ubuntu).

主应用程序:

from sonip.api.factory import create_app
app = create_app()
def main():
    app.run(debug=True, threaded=True)
if __name__ == "__main__":
    main()

Flask应用程序的实际设置是在工厂完成的,包括注册 blueprint 等。

The actual set up of the Flask app is done in a factory including registering blueprint, etc.

def create_app():
    app = Flask(__name__)
    app.config['SERVER_NAME'] = settings.FLASK_SERVER_NAME
    app.config['SWAGGER_UI_DOC_EXPANSION'] = settings.RESTPLUS_SWAGGER_UI_DOC_EXPANSION
    app.config['RESTPLUS_VALIDATE'] = settings.RESTPLUS_VALIDATE
    app.config['RESTPLUS_MASK_SWAGGER'] = settings.RESTPLUS_MASK_SWAGGER
    app.config['ERROR_404_HELP'] = settings.RESTPLUS_ERROR_404_HELP
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://{}:{}@{}:{}/{}".format(
        settings.DB_USER,
        settings.DB_PASS,
        settings.DB_HOST,
        settings.DB_PORT,
        settings.DB_NAME)
    db.init_app(app)
    blueprint = Blueprint('api', __name__, url_prefix='/api')
    app.register_blueprint(blueprint)

    return app

当我运行 python application.py 并使用 curl -X GET http: // localhost:5000 / api ,它将返回正确的Swagger页面。但是,如果我尝试通过为外部流量指定 host = 0.0.0.0 来运行应用程序,则对于相同的请求,我会收到404。

When I run python application.py and use curl -X GET http://localhost:5000/api, it returns the correct Swagger page. However, if I tried to run the app by specifying host=0.0.0.0 for external traffic, I got 404 for the same request.

(env) ubuntu@ip-172-31-18-136:~/aae$ python application.py
 * Serving Flask app "sonip.api.factory" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://localhost:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 948-062-124
127.0.0.1 - - [16/May/2018 18:07:24] "GET /api/ HTTP/1.1" 200 -
♥(env) ubuntu@ip-172-31-18-136:~/aae$ vi application.py
(env) ubuntu@ip-172-31-18-136:~/aae$ python application.py
 * Serving Flask app "sonip.api.factory" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 948-062-124
165.225.34.185 - - [16/May/2018 18:08:28] "GET /api/ HTTP/1.1" 404 -

端口5000已打开,以允许安全组中的所有入站流量。我尝试了仅用几行代码的Vanilla Flask应用程序,效果很好。

Port 5000 is open to allow all inbound traffic in the security group. I tried a vanilla Flask app with just a few line of code, it worked just fine.

app = Flask(__name__)
if __name__ == '__main__':
    app.run(debug=True, port=8080, host='0.0.0.0')

这至少意味着5000很好是蓝图还是 Swagger

This at least means 5000 is fine. Could it be the Blueprint or Swagger?

推荐答案

设置 SERVER_NAME 并将主机/端口更改为 app.run()曾经是解决问题的良方。

Setting SERVER_NAME and changing the host/port to something different in app.run() used to be a recipe for problems. At best, it's under-documented.

尝试将 settings.FLASK_SERVER_NAME 更改为 0.0。 0.0:5000 。或者,如果您的应用希望使用Cookie,请尝试使用 something.dev:5000 并为 something.dev 到本地 / etc / hosts

Try changing settings.FLASK_SERVER_NAME to 0.0.0.0:5000. Or if your app wants to be using cookies, try the trick of using something.dev:5000 and adding an entry for something.dev to your local /etc/hosts.

这篇关于在0.0.0.0上运行Flask应用程序时为什么会得到404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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