折腾:
【已解决】把Python3的Flask部署到远程CentOS7服务器
期间,Flask的app,用gunicorn去运行,是可以运行的,但是客户端去访问api出错:
[root@naturling-general-01 robotDemo]# pipenv shell
Warning: Your Pipfile requires python_version 3.6, but you are using 3.4.5 (/root/.local/share/v/r/bin/python).
$ pipenv check will surely fail.
Spawning environment shell (/bin/bash). Use ‘exit’ to leave.
[root@naturling-general-01 robotDemo]# . /root/.local/share/virtualenvs/robotDemo-dwdcgdaG/bin/activate
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# ll
total 40
-rw-r–r– 1 root root 8516 Apr 23 10:27 app.py
-rw-r–r– 1 root root 1700 Apr 23 17:28 gunicorn_config.py
-rw-r–r– 1 root root 212 Apr 20 10:15 Pipfile
-rw-r–r– 1 root root 7936 Apr 23 11:37 Pipfile.lock
-rw-r–r– 1 root root 10317 Apr 23 10:30 supervisord.conf
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# mkdir logs
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# ll
total 44
-rw-r–r– 1 root root 8516 Apr 23 10:27 app.py
-rw-r–r– 1 root root 1700 Apr 23 17:28 gunicorn_config.py
drwxr-xr-x 2 root root 4096 Apr 23 17:45 logs
-rw-r–r– 1 root root 212 Apr 20 10:15 Pipfile
-rw-r–r– 1 root root 7936 Apr 23 11:37 Pipfile.lock
-rw-r–r– 1 root root 10317 Apr 23 10:30 supervisord.conf
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# cd logs
(robotDemo-dwdcgdaG) [root@naturling-general-01 logs]# cd ..
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# ll
total 44
-rw-r–r– 1 root root 8516 Apr 23 10:27 app.py
-rw-r–r– 1 root root 1700 Apr 23 17:28 gunicorn_config.py
drwxr-xr-x 2 root root 4096 Apr 23 17:45 logs
-rw-r–r– 1 root root 212 Apr 20 10:15 Pipfile
-rw-r–r– 1 root root 7936 Apr 23 11:37 Pipfile.lock
-rw-r–r– 1 root root 10317 Apr 23 10:30 supervisord.conf
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# gunicorn
gunicorn gunicorn_paster
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# gunicorn -c gunicorn_config.py app:app
currentRootPath=/root/naturling_20180101/web/server/robotDemo
[2018-04-23 17:46:18,376 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f5202e90cf8>
[2018-04-23 17:46:18,380 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-23 17:46:18,434 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f5202e90d68>
[2018-04-23 17:46:18,437 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-23 17:46:18,461 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f5202e91da0>
[2018-04-23 17:46:18,464 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-23 17:46:18,475 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f5202e91e10>
[2018-04-23 17:46:18,477 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-23 17:46:18,586 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f5202e91e80>
[2018-04-23 17:46:18,588 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-23 17:46:18,644 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f5202e91ef0>
[2018-04-23 17:46:18,646 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-23 17:46:18,691 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f5203117f98>
[2018-04-23 17:46:18,694 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-23 17:46:18,742 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f5202e8ffd0>
[2018-04-23 17:46:18,744 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-23 17:46:18,785 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f5202e98080>
[2018-04-23 17:46:18,787 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
但是访问端口失败:
org.apache.http.conn.HttpHostConnectException: Connect to x.x.x.x:port [/x.x.x.x] failed: Operation timed out (Connection timed out)
想起来了,是阿里云的端口问题,所以去加上安全组的规则:
port
然后再去试试API访问
结果错误依旧:
org.apache.http.conn.HttpHostConnectException: Connect to x.x.x:port [/x.x.x.x] failed: Connection refused (Connection refused)
难道是:
此处app的run中,没有加上:
host="0.0.0.0"
去加上试试
问题依旧,去看log:
cat logs/gunicorn_error.log
[2018-04-23 17:58:09 +0800] [22119] [INFO] Listening at: http://127.0.0.1:32851 (22119)
[2018-04-23 17:58:09 +0800] [22119] [INFO] Using worker: threads
[2018-04-23 17:58:09 +0800] [22122] [INFO] Booting worker with pid: 22122
好像是:
实际上此处内部还只是:http://127.0.0.1
不允许外部访问?
gunicorn flask Connect to failed Connection refused
flask + nginx = connection refused – Stack Overflow
python – Flask – Gunicorn returns 404 after curl to localhost on same instance – Stack Overflow
502 Bad Gateway Ngnix & Gunicorn | DigitalOcean
Getting nginx proxy to work properly with gunicorn/flask – Server Fault
换postman试试
直接没有响应
Could not get any response
There was an error connecting to http://x.x.x.x:yyy/qa?q=play%20a%20happy%20song.
Why this might have happened:
* The server couldn’t send a response:
Ensure that the backend is working properly
* Self-signed SSL certificates are being blocked:
Fix this by turning off ‘SSL certificate verification’ in Settings > General
* Proxy configured incorrectly
Ensure that proxy is configured correctly in Settings > Proxy
* Request timeout:
Change request timeout in Settings > General
flask deploy Connect to failed Connection refused
deployment – Nginx and uWSGI Flask app Connection Refused – Server Fault
看到都提到了nginx
-》难道部署是,必须使用nginx?
Connection refused with uwsgi and nginx | DigitalOcean
python – Can’t deploy Flask with Nginx – Stack Overflow
“ either permission problem or nothing running on that port”
看来就是:
端口问题或者是权限问题
Fix for Connection Reset on POST | Flask (A Python Microframework)
flask deploy Could not get any response
I’m having issues using nginx to route my Flask app. What am I doing wrong? | DigitalOcean
python – Configure Flask dev server to be visible across the network – Stack Overflow
python 2.7 – Flask web app not responding to external requests on EC2 – Stack Overflow
ubuntu – Unable to Connect to Flask App On Docker From Host – Stack Overflow
结果log中还是127的:
[2018-04-24 09:07:43 +0800] [23413] [INFO] Listening at: http://127.0.0.1:32851 (23413)
[2018-04-24 09:07:43 +0800] [23413] [INFO] Using worker: threads
[2018-04-24 09:07:43 +0800] [23416] [INFO] Booting worker with pid: 23416
算了,手动运行自己的flask的app试试:
python app.py
可以运行的,但是出错:
【已解决】pymongo的count()出错:pymongo.errors.ServerSelectionTimeoutError timed out
所以现在问题变成:
手动执行是可以的:
python app.py
但是换成gunicorn运行,结果却访问被拒绝了。
gunicorn deploy flask connection refused
python – Why can’t I connect to Flask server when hosted remotely? – Stack Overflow
python – Why shouldn’t Flask be deployed with the built in server? – Stack Overflow
python – Configure Flask dev server to be visible across the network – Stack Overflow
然后才想到:
去看gunicorn的配置
发现了,gunicorn中配置的是:
bind = ‘127.0.0.1:32851’ #绑定ip和端口号
应该就是这个127.0.0.1,导致了不允许外部访问。
所以去改为:
#bind = ‘127.0.0.1:32851’ #绑定ip和端口号
bind = ‘0.0.0.0:32851’ #绑定ip和端口号
然后再去试试
gunicorn -c gunicorn_config.py app:app
结果就可以了。
【总结】
此处对于在服务器中:
Flask的app,单独手动:
python app.py
是可以正常运行,然后远程Mac本地是可以访问服务器上的接口的。
而换成gunicorn后去运行,远程访问接口就出错:
org.apache.http.conn.HttpHostConnectException Connect to failed Operation timed out
org.apache.http.conn.HttpHostConnectException: Connect to failed: Connection refused (Connection refused)
Could not get any response
原因是:
此处阿里云服务器的端口号也没有开放
Flask的app中的host也不是0.0.0.0
gunicorn中host是127.0.0.1
解决办法:
依次解决掉:
(1)去阿里云ECS的安全组中,加上允许入方向访问端口
(2)把Flask中app的host改为0.0.0.0
app.py
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=SERVER_PORT,
debug=DEBUG
)
(3)把gunicorn中的host改为0.0.0.0
gunicorn_config.py
#bind = ‘127.0.0.1:32851’ #绑定ip和端口号
bind = ‘0.0.0.0:32851’ #绑定ip和端口号
转载请注明:在路上 » 【已解决】gunicorn运行Flask的app但访问出错:org.apache.http.conn.HttpHostConnectException Connect to failed Operation timed out