Python 从我的公共 IP 地址看不到 Flask 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36597643/
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 18:04:52  来源:igfitidea点击:

Flask server not visible from my public ip address

pythonflaskwebserver

提问by Keatinge

I'm trying to run a flask server on my desktop PC that is publicly available on the internet. I've done the following:

我正在尝试在我的台式电脑上运行一个可在互联网上公开获得的 Flask 服务器。我做了以下工作:

I'm using the following code as a test webserver

我使用以下代码作为测试网络服务器

from flask import Flask, request, redirect
app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Test 123 "

if __name__ == "__main__":
    app.run(host="0.0.0.0", port="33")

When I open my browser to: http://192.168.1.11:33/the page displays properly, I see "Test 123"

当我打开浏览器到:http: //192.168.1.11: 33/页面正确显示时,我看到“Test 123”

My problem comes when trying to connect to my webserver from my public ip addressWhen I open my browser to http://xx.xxx.xxx.xx:30(my ip address) all I see is "this site can't be reached, xx.xxx.xxx.xx refused to connect"

试图从我的公网IP地址连接到我的网络服务器时,我的问题就来了。当我打开我的浏览器//xx.xxx.xxx.xx:HTTP 30(我的IP地址)所有我看到的是“这个网站不能已到达,xx.xxx.xxx.xx 拒绝连接”

I've looked up all the stack overflow answers, I've done the following:

我已经查找了所有堆栈溢出的答案,我已经完成了以下操作:

  • Turned off windows firewall
  • Changed host from "192.168.1.11" to "0.0.0.0"
  • Tried a different port
  • 关闭windows防火墙
  • 将主机从“192.168.1.11”更改为“0.0.0.0”
  • 尝试了不同的端口

screenshot of code running and error shown: http://i.imgur.com/a05GvEs.png

代码运行和错误显示的屏幕截图:http: //i.imgur.com/a05GvEs.png

My question is: What do I need to do to make my flask server visible from my public ip address?

我的问题是:我需要做什么才能让我的 Flask 服务器从我的公共 IP 地址可见?

回答by Kreet.

Do you have DHCP activated on your router? If yes do you see your host as 192.168.1.11 in there?

您的路由器上是否启用了 DHCP?如果是,您是否在那里看到您的主机为 192.168.1.11?

You have to use '0.0.0.0' on host, that tells Flask to listen on all addresses. Try specifying the port with quotes as app.run(host="0.0.0.0", port="33")

您必须在主机上使用“0.0.0.0”,它告诉 Flask 侦听所有地址。尝试用引号将端口指定为app.run(host="0.0.0.0", port="33")

回答by ganeshredcobra

change it to app.run(host= '0.0.0.0', port="33") to run on your machines IP address.

将其更改为 app.run(host='0.0.0.0', port="33") 以在您的机器 IP 地址上运行。

Documented on the Flask site under "Externally Visible Server" on the Quickstart page: http://flask.pocoo.org/docs/0.10/quickstart/#a-minimal-application

在快速入门页面上的“外部可见服务器”下的 Flask 站点上记录:http: //flask.pocoo.org/docs/0.10/quickstart/#a-minimal-application

Add port forwarding to port 33 in your router Port forwarding explained here http://www.howtogeek.com/66214/how-to-forward-ports-on-your-router/

将端口转发添加到路由器中的端口 33 此处解释了端口转发 http://www.howtogeek.com/66214/how-to-forward-ports-on-your-router/

回答by Ramesh Seera

You must give the public ip address/LAN ip address as an argument to app.run method. When you don't provide host argument, it works fine with http://localhost:8888/and http://127.0.0.1:888/, but not to access outside the system where you are running the REST services

您必须将公共 ip 地址/局域网 ip 地址作为 app.run 方法的参数。当您不提供主机参数时,它适用于http://localhost:8888/http://127.0.0.1:888/,但不能在您运行 REST 服务的系统外部访问

Following is the example. app.run(host="192.168.0.29",debug=True, port=8888)

以下是示例。app.run(host="192.168.0.29",debug=True, port=8888)

回答by user11196966

Every webservice should be run from different port address.Single service is running from a single port.

每个 webservice 应该从不同的端口地址运行。单个服务从单个端口运行。

回答by Pankus

You must try and use the same ip of the development server. Thus, for instance, if the dev server is running on a PC with the address 192.168.1.11and port 33, other clients must point at the same address: 192.168.1.11:33. As far as my small experience, it works with the debugger disabled, but I did not check if this is an essential prerequisite.

您必须尝试使用​​与开发服务器相同的 IP。因此,例如,如果开发服务器在具有地址192.168.1.11和端口的 PC 上运行33,则其他客户端必须指向相同的地址:192.168.1.11:33。就我的小经验而言,它可以在禁用调试器的情况下工作,但我没有检查这是否是必不可少的先决条件。

good luck

祝你好运