Python 如何在配置模块中设置 Flask 应用程序的主机和端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49332853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:03:22  来源:igfitidea点击:

How set the host and the port of a Flask app in config module?

pythonflask

提问by

To execute an application in Flask we use the method

要在 Flask 中执行应用程序,我们使用方法

app.run ()

In this method we can pass configuration parameters, among them:

在这个方法中我们可以传递配置参数,其中:

host, port, debug

By default the Flask application is executed in

默认情况下,Flask 应用程序在

host = localhost
port = 5000

So, if we want to change that configuration, we pass it to the run method

所以,如果我们想改变那个配置,我们把它传递给 run 方法

app.run (host = "10.100.100.10", port = 9566)

In the case of wanting to have different configurations for your application (either, production, development, testing) you must define a configuration dictionary, something like this:

如果想要为您的应用程序(生产、开发、测试)使用不同的配置,您必须定义一个配置字典,如下所示:

app.config.update (
    DEBUG = True,
    SECRET_KEY = '...'
)

Or more specified create a config module:

或者更指定创建一个配置模块:

class Base(object):
    DEBUG = False
    SECRET_KEY = "Shhhh is a secret"
    SERVER_NAME="localhost:5000"

class Develop(Base):
    SERVER_NAME="10.100.100.10:5001"

Reading (http://flask.pocoo.org/docs/0.12/config/) I found this attribute:

阅读(http://flask.pocoo.org/docs/0.12/config/)我发现了这个属性:

SERVER_NAME

Quoting the Flask documentation:

引用 Flask 文档:

the name and port number of the server. Required for subdomain support (e.g .: 'myapp.dev:5000') Note that localhost does not support subdomains so setting this to "localhost" does not help. Setting to SERVER_NAME also by default enables URL generation without a request context but with an application context.

服务器的名称和端口号。需要子域支持(例如 .: 'myapp.dev:5000') 请注意,localhost 不支持子域,因此将其设置为“localhost”无济于事。默认情况下,设置为 SERVER_NAME 也会在没有请求上下文但有应用程序上下文的情况下启用 URL 生成。

This attribute should change the host and the port.

此属性应更改主机和端口。

I defined this then:

我当时定义了这个:

app.config.update (
    DEBUG = True,
    SERVER_NAME = "10.100.100.10:6500"
)

and

class Base(object):
    DEBUG = False
    SECRET_KEY = "Shhhh is a secret"
    SERVER_NAME="localhost:5000"

class Develop(Base):
    SERVER_NAME="10.100.100.10:5001"

and

app = Flask(__name__)

app.config.from_object('config.Develop')

As a result it keeps showing when executing the application:

因此,它在执行应用程序时不断显示:

Running in 127.0.0.1:5000

How can I make my application run on another host and port without having to use parameters in the run method?

如何让我的应用程序在另一个主机和端口上运行而不必在 run 方法中使用参数?

回答by Emre

why do you need to set the port outside the run method? Don't forget such a deployment is only for development purposes. in your built in server but you may change the port like that: somewhere you should have something like app.run()I am posting a runnable code, its port set to 5001. I hope it helps.

为什么需要在run方法之外设置端口?不要忘记这样的部署仅用于开发目的。在您的内置服务器中,但您可以像这样更改端口:在某个地方您应该有类似app.run()我发布可运行代码的内容,其端口设置为 5001。我希望它有所帮助。

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run(port=5001)