问题现象:
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)