0

是否可以在生产期间在 Flask 中创建一个虚拟服务器名称?例如,我想要以下内容:www.foo.com用于开发。

def main():
    app.run(debug=True, host='foo.com', port=8000)

这样的路线:

@app.route('/bar/')
def bar() -> str:
    return 'bar'

将在 到达foo.com/bar/。我试过改变配置:

app.config['SERVER_NAME'] = 'foo.com'

但我得到一个OSError: [Errno 49] Can't assign requested address

4

2 回答 2

0

更改主机参数

app.run(debug=True, host='0.0.0.0', port=8000)

它定义了服务器将侦听的 IP 地址,而不是 DNS。有关更多说明,请参见链接

于 2019-11-08T16:49:01.613 回答
0

在您的开发 PC 主机文件中添加一个foo.com指向本地 IP (127.0.0.1) 的条目。例如,在 Windows 上,hosts 文件被调用hosts并位于C:\Windows\System32\drivers\etc标准安装中。

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host


127.0.0.1 foo.com

然后,您将能够在开发时提供您的 Flask 页面foo.com:8000

于 2019-11-08T17:54:13.817 回答