为什么我在 0.0.0.0 上的 运行 Flask 应用程序时得到 404
Why do I get 404 when running Flask app on 0.0.0.0
我正在 AWS EC2 实例 (Ubuntu) 上测试我的 Flask 应用程序。
主应用程序:
from sonip.api.factory import create_app
app = create_app()
def main():
app.run(debug=True, threaded=True)
if __name__ == "__main__":
main()
Flask 应用程序的实际设置是在工厂完成的,包括注册 blueprint
等
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
时,它 returns 正确的 Swagger 页面。但是,如果我尝试通过为外部流量指定 host=0.0.0.0
来 运行 应用程序,我会收到相同请求的 404。
(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 已打开以允许安全组中的所有入站流量。我尝试了一个只有几行代码的普通 Flask 应用程序,它工作得很好。
app = Flask(__name__)
if __name__ == '__main__':
app.run(debug=True, port=8080, host='0.0.0.0')
这至少意味着 5000 是可以的。可能是 Blueprint
或 Swagger
?
设置 SERVER_NAME
并将 host/port 更改为 app.run()
中的不同内容曾经是问题的根源。充其量,它的文档不足。
尝试将 settings.FLASK_SERVER_NAME
更改为 0.0.0.0:5000
。或者,如果您的应用想要使用 cookie,请尝试使用 something.dev:5000
并将 something.dev
的条目添加到您的本地 /etc/hosts
.
我正在 AWS EC2 实例 (Ubuntu) 上测试我的 Flask 应用程序。
主应用程序:
from sonip.api.factory import create_app
app = create_app()
def main():
app.run(debug=True, threaded=True)
if __name__ == "__main__":
main()
Flask 应用程序的实际设置是在工厂完成的,包括注册 blueprint
等
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
时,它 returns 正确的 Swagger 页面。但是,如果我尝试通过为外部流量指定 host=0.0.0.0
来 运行 应用程序,我会收到相同请求的 404。
(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 已打开以允许安全组中的所有入站流量。我尝试了一个只有几行代码的普通 Flask 应用程序,它工作得很好。
app = Flask(__name__)
if __name__ == '__main__':
app.run(debug=True, port=8080, host='0.0.0.0')
这至少意味着 5000 是可以的。可能是 Blueprint
或 Swagger
?
设置 SERVER_NAME
并将 host/port 更改为 app.run()
中的不同内容曾经是问题的根源。充其量,它的文档不足。
尝试将 settings.FLASK_SERVER_NAME
更改为 0.0.0.0:5000
。或者,如果您的应用想要使用 cookie,请尝试使用 something.dev:5000
并将 something.dev
的条目添加到您的本地 /etc/hosts
.