【问题标题】:Dummy server name in flask development烧瓶开发中的虚拟服务器名称
【发布时间】:2019-11-08 15:06:55
【问题描述】:

是否可以在生产期间在 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] 无法分配请求的地址

【问题讨论】:

    标签: python flask server-name


    【解决方案1】:

    更改主机参数

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

    它定义了服务器将监听的 IP 地址,而不是 DNS。更多解释见link

    【讨论】:

      【解决方案2】:

      在您的开发 PC 主机文件中添加一个指向本地 IP (127.0.0.1) 的 foo.com 条目。例如,在 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
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-07
        • 2023-03-19
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多