gpt4 book ai didi

Google Compute Engine 上的 Flask - 无法从外部/浏览器访问

转载 作者:行者123 更新时间:2023-12-05 00:12:02 25 4
gpt4 key购买 nike

我在我的 Compute Engine 上设置了一个简单的 Flask 服务器,它在 127.0.0.1:5000 上内部运行。

如果我通过 ssh 连接到该实例并 curl 127.0.0.1:5000 我会看到 hello world 消息并且 Flask 实例在其日志中显示一个 get。

但是,如果我通过浏览器使用外部 IP 地址,它会说该站点不存在。

我尝试设置多个差异防火墙,指向 0.0.0.0/0 或 127.0.0.1/0 并包括 tcp:5000 和 udp,或者只是 tcp 或只是说允许所有端口。

什么都行不通。

我确实尝试运行 netstat -plant,它说端口 5000 连接到 python3(这是我的 Flask 实例),但端口已关闭。

更新:

让它正常工作,但只会添加更多日志记录/输出,以防其他人也遇到此问题。

me@instance-1:~$ nmap 1.2.3.4
Starting Nmap 7.40 ( https://nmap.org ) at 2018-08-19 15:30 UTC
Nmap scan report for 4.3.2.1.bc.googleusercontent.com (1.2.3.4)
Host is up (0.0016s latency).
Not shown: 995 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp closed http
443/tcp closed https
3389/tcp closed ms-wbt-server
5000/tcp closed upnp

me@instance-1:~$ netstat -plant
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 29414/python3
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 1 10.142.0.2:22 118.24.87.43:51760 LAST_ACK -
tcp 0 0 10.142.0.2:41788 169.254.169.254:80 CLOSE_WAIT -
tcp 0 0 10.142.0.2:41796 169.254.169.254:80 ESTABLISHED -
tcp 0 0 127.0.0.1:55258 127.0.0.1:5000 TIME_WAIT -
tcp 0 0 10.142.0.2:41794 169.254.169.254:80 ESTABLISHED -
tcp 0 0 10.142.0.2:22 74.125.73.96:47952 ESTABLISHED -
tcp 0 816 10.142.0.2:22 74.125.73.36:51834 ESTABLISHED -
tcp 0 0 10.142.0.2:41792 169.254.169.254:80 ESTABLISHED -
tcp6 0 0 :::22 :::* LISTEN -

然后我使用重新启动 flask
flask run --host=0.0.0.0 --port=5000
现在...

me@instance-1:~$ netstat -plant
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 31365/python3
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 10.142.0.2:45802 169.254.169.254:80 ESTABLISHED -
tcp 0 0 127.0.0.1:55258 127.0.0.1:5000 TIME_WAIT -
tcp 0 0 10.142.0.2:41794 169.254.169.254:80 CLOSE_WAIT -
tcp 0 0 10.142.0.2:45804 169.254.169.254:80 ESTABLISHED -
tcp 0 0 10.142.0.2:22 74.125.73.96:47952 ESTABLISHED -
tcp 0 0 10.142.0.2:45800 169.254.169.254:80 ESTABLISHED -
tcp 0 816 10.142.0.2:22 74.125.73.36:51834 ESTABLISHED -
tcp6 0 0 :::22 :::* LISTEN -
me@instance-1:~$ nmap 1.3.3.4
Starting Nmap 7.40 ( https://nmap.org ) at 2018-08-19 19:24 UTC
Nmap scan report for 4.3.2.1.bc.googleusercontent.com (1.2.3.4)
Host is up (0.0012s latency).
Not shown: 995 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp closed http
443/tcp closed https
3389/tcp closed ms-wbt-server
5000/tcp open upnp
Nmap done: 1 IP address (1 host up) scanned in 4.75 seconds

最佳答案

我假设您使用的是默认运行方法,即提供主机地址为 0.0.0.0,它会起作用

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
return 'Hi'

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)

这将允许您使用 ip 地址从本地主机访问您的网站。

如果您使用 flask 运行方法,则将其运行为
flask run --host=0.0.0.0 --port=5000

关于Google Compute Engine 上的 Flask - 无法从外部/浏览器访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51919567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com