ecs服务器搭建flask应用,无法通过公网ip访问网站

问题现象:

ecs用的是专有网络,确认已开启80和5000端口,地址段是0.0.0.0/0。
ssh进到ecs之后启动flask应用用的默认地址127.0.0.1:5000,也尝试过把端口改成80,通过 服务器公网ip:端口 尝试访问网站均显示无法连接到服务器。
启动flask应用后通过netstat确认5000端口在listen状态,仍然无法访问

规避方法:

flask默认只能本机访问,要让网络上其他机器访问,需要增加host为0.0.0.0

flask官网原文(http://flask.pocoo.org/docs/0.12/quickstart/#a-minimal-application):
Externally Visible Server

If you run the server you will notice that the server is only accessible from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer.

If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:

flask run --host=0.0.0.0

This tells your operating system to listen on all public IPs.

测试如下:
app.run(host="0.0.0.0", port=80)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值