Why do I get 404 when running Flask app on 0.0.0.0

£可爱£侵袭症+ 提交于 2019-12-11 06:21:11

问题


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

Main app:

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

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

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 -

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')

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/50377752/why-do-i-get-404-when-running-flask-app-on-0-0-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!