【问题标题】:FLASK - How to configure port using app.config['PORT']?FLASK - 如何使用 app.config ['PORT'] 配置端口?
【发布时间】:2021-04-28 00:42:40
【问题描述】:

所以我按照this 在线教程建立了一个可以在我的本地网络上完美运行的站点。现在我正在尝试在 Google 的 App Engine 上部署它,但遇到了错误。 基本上我不知道如何更改我的烧瓶应用程序正在运行的端口(不使用 app.run() )。

我实际上只是从教程中复制粘贴了代码并进行了一些调整,所以我不确定如何使用代码设置 eport。由于本教程从未使用过任何 app.run(port=8080) 并且该页面可以正常工作,因此我不确定是否要介绍那段代码。但是,代码确实使用 app.config[] 来设置 SECRET_KEY 例如。而且我尝试使用相同的方法来设置 PORT,但它不起作用。怎么会这样??

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager

#init SQLAlchemy so we can use it later in our models
db = SQLAlchemy()


def create_app():
    app = Flask(__name__)
    app.config['PORT'] = 8080
    app.config['SECRET_KEY'] = b'someweirdshit'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'

    db.init_app(app)

    login_manager = LoginManager()
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app)

    from .models import User

    @login_manager.user_loader
    def load_user(user_id):
        # since the user id is just the primary key of our user table, use it in the query for the user
        return User.query.get(int(user_id))

    #blueprint for auth routes in our app
    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)

    #blueprint for non-auth parts of app
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

上面的代码只是我的 Flask 应用程序中使用的脚本之一。我知道它正在执行,因为应用程序成功连接并使用了第 13 行中配置的 db.sqlite 数据库。但是第 11 行(我尝试配置端口的位置)似乎不起作用。

【问题讨论】:

    标签: google-app-engine flask


    【解决方案1】:
    1. 使用app.run(host='127.0.0.1', port=<PORT_NUMBER>, debug=True) 没有任何问题。如果您走这条路,那么您最终只需从项目文件夹中执行python main.py 即可运行您的应用程序。

    2. 如果您仍然不想使用app.run,那么您应该使用FLASK_RUN_PORT 来指定您的端口号(参见Flask Documentation)

        $ export FLASK_RUN_PORT=8000
        $ flask run
     
    
    1. 如果您不想处理命令 python main.py 或 flask run,那么您可以使用 GAE GUI(例如我们的 - https://nocommandline.com)来管理您的代码(即在本地运行并将其部署到生产)。使用 GUI,您可以单击一个按钮在本地运行文件,然后单击另一个按钮部署到生产环境。如果您的代码是用Python2 编写的,则不必在代码中包含端口号。您只需在 GUI UI 上指定端口号,GCloud's dev_appserver.py(GUI 在后台使用)将负责附加端口号。如果是 Python3,那么您必须包含类似的代码
        if __name__ == '__main__':
            app.run(host='127.0.0.1', port=<PORT_NUMBER>, debug=True)
    

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      相关资源
      最近更新 更多