最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Flask的Python服务外网无法访问

Flask crifan 13243浏览 0评论

CentOS中,之前用Flask启动了一个服务:

gunicorn -w 4 -b 127.0.0.1:21084 run:app &

访问接口,返回内容

但是是内网才可以访问:

(RunningFast) ➜  RunningFast curl 127.0.0.1:21084
Hello, Running Fast!#         

外网,无法访问:

115.29.173.126:21084

api测试接口:

然后后来找别人帮忙,去试了试:

把防火墙关闭了,也还是不行。

把SELinux关闭了,也不行

然后注意到:

其自己的java的服务8998,外网是可以访问的:

然后注意到,其所用的IP地址是0.0.0.0

而自己用的是:127.0.0.0

(RunningFast) ➜  RunningFast netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:8998                0.0.0.0:*                   LISTEN      16107/java          
tcp        0      0 0.0.0.0:10022               0.0.0.0:*                   LISTEN      17286/docker-proxy  
tcp        0      0 0.0.0.0:8999                0.0.0.0:*                   LISTEN      16107/java          
tcp        0      0 0.0.0.0:9000                0.0.0.0:*                   LISTEN      19706/docker-proxy  
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17851/mysqld        
tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      19659/redis-server  
tcp        0      0 0.0.0.0:9003                0.0.0.0:*                   LISTEN      19669/docker-proxy  
tcp        0      0 127.0.0.1:8080              0.0.0.0:*                   LISTEN      24318/python        
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1335/sshd           
tcp        0      0 0.0.0.0:3000                0.0.0.0:*                   LISTEN      17240/docker-proxy  
tcp        0      0 0.0.0.0:8088                0.0.0.0:*                   LISTEN      29962/docker-proxy  
tcp        0      0 0.0.0.0:5432                0.0.0.0:*                   LISTEN      18421/docker-proxy  
tcp        0      0 127.0.0.1:21084             0.0.0.0:*                   LISTEN      28384/python        
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      19505/docker-proxy  
udp        0      0 192.168.42.1:123            0.0.0.0:*                               1346/ntpd           
udp        0      0 115.29.173.126:123          0.0.0.0:*                               1346/ntpd           
udp        0      0 10.161.170.247:123          0.0.0.0:*                               1346/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               1346/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               1346/ntpd           
(RunningFast) ➜  RunningFast netstat -tulpn | grep java
tcp        0      0 0.0.0.0:8998                0.0.0.0:*                   LISTEN      16107/java          
tcp        0      0 0.0.0.0:8999                0.0.0.0:*                   LISTEN      16107/java          
(RunningFast) ➜  RunningFast netstat -tulpn | grep python
tcp        0      0 127.0.0.1:8080              0.0.0.0:*                   LISTEN      24318/python        
tcp        0      0 127.0.0.1:21084             0.0.0.0:*                   LISTEN      28384/python     

所以,去把自己的IP也换为0.0.0.0试试

本地还是OK的:

(RunningFast) ➜  RunningFast curl http://localhost:21084   

<div–<——————————————————————————

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:49]:
got into test root endpoint: /

<div–<——————————————————————————

Hello, Running Fast!#     

外网访问也可以了:

115.29.173.126:21084

115.29.173.126:21084/runningfast/api/v1.0/tasks

所以就要去搞清楚:

【已解决】服务器中启动服务时候的IP选择:127.0.0.1 vs 0.0.0.0

【后记1】

另外,据说:

设置为服务器(外网)的IP地址,也是可以的?

去试试:

果然是可以的:

(RunningFast) ➜  RunningFast gunicorn -w 4 -b 115.29.173.126:21084 run:app &
[1] 30347
(RunningFast) ➜  RunningFast [2016-09-29 16:44:09 +0000] [30347] [INFO] Starting gunicorn 19.6.0
[2016-09-29 16:44:09 +0000] [30347] [INFO] Listening at: http://115.29.173.126:21084 (30347)
[2016-09-29 16:44:09 +0000] [30347] [INFO] Using worker: sync
[2016-09-29 16:44:09 +0000] [30356] [INFO] Booting worker with pid: 30356
[2016-09-29 16:44:09 +0000] [30357] [INFO] Booting worker with pid: 30357
[2016-09-29 16:44:09 +0000] [30362] [INFO] Booting worker with pid: 30362
[2016-09-29 16:44:09 +0000] [30363] [INFO] Booting worker with pid: 30363

<div–<——————————————————————————

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:49]:
got into test root endpoint: /

<div–<——————————————————————————

可以访问:

【后记2】

看到:

部署 | Flask之旅

-》才知道:

实际上,此处是为了调试,是把监听IP设置为0.0.0.,可以接受来自外部的请求

-》在上线之后,应该不暴露Flask,而是监听127.0.0.1,让代理服务器,比如Nginx,把外部请求,转发给Flask,才是最安全的

-》否则暴露的Flask,很容易受到DDOS攻击

转载请注明:在路上 » 【已解决】Flask的Python服务外网无法访问

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.167 seconds, using 22.14MB memory