折腾:
【记录】用Flask实现测评系统的后端
期间,需要把现有的Flask代码,先部署到在线服务器中,
以及确保小程序端能调用和访问
->意味着要支持https
-》需要更新nginx配置,支持https的另外一个接口。
本地去安装额外的库:
➜ xxx git:(master) ✗ pipenv install numpy Installing numpy… Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting numpy Downloading https://pypi.tuna.tsinghua.edu.cn/packages/74/68/2b00ba3c7390354db2a1706291750b6b7e911f6f79c0bd2184ae04f3c6fd/numpy-1.15.4-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (24.5MB) Installing collected packages: numpy Successfully installed numpy-1.15.4 Adding numpy to Pipfile's [packages]… Pipfile.lock (d06c53) out of date, updating to (28e8c6)… Locking [dev-packages] dependencies… Locking [packages] dependencies… ^C /Users/crifan/Library/Python/2.7/lib/python/site-packages/pipenv/vendor/vistir/compat.py:109: ResourceWarning: Implicitly cleaning up <TemporaryDirectory '/var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/pipenv-T84JBt-requirements'> warnings.warn(warn_message, ResourceWarning) Aborted! ^C ➜ xxx git:(master) ✗ ➜ xxx git:(master) ✗ pipenv graph Flask-Cors==3.0.7 - Flask [required: >=0.9, installed: 1.0.2] - click [required: >=5.1, installed: 7.0] - itsdangerous [required: >=0.24, installed: 1.1.0] - Jinja2 [required: >=2.10, installed: 2.10] - MarkupSafe [required: >=0.23, installed: 1.1.0] - Werkzeug [required: >=0.14, installed: 0.14.1] - Six [required: Any, installed: 1.12.0] Flask-PyMongo==2.2.0 - Flask [required: >=0.11, installed: 1.0.2] - click [required: >=5.1, installed: 7.0] - itsdangerous [required: >=0.24, installed: 1.1.0] - Jinja2 [required: >=2.10, installed: 2.10] - MarkupSafe [required: >=0.23, installed: 1.1.0] - Werkzeug [required: >=0.14, installed: 0.14.1] - PyMongo [required: >=3.0, installed: 3.7.2] Flask-RESTful==0.3.7 - aniso8601 [required: >=0.82, installed: 4.0.1] - Flask [required: >=0.8, installed: 1.0.2] - click [required: >=5.1, installed: 7.0] - itsdangerous [required: >=0.24, installed: 1.1.0] - Jinja2 [required: >=2.10, installed: 2.10] - MarkupSafe [required: >=0.23, installed: 1.1.0] - Werkzeug [required: >=0.14, installed: 0.14.1] - pytz [required: Any, installed: 2018.7] - six [required: >=1.3.0, installed: 1.12.0] gevent==1.3.7 - greenlet [required: >=0.4.14, installed: 0.4.15] gunicorn==19.9.0 numpy==1.15.4 python-dotenv==0.10.1
中间lock太长时间,中断了,去看看,已安装numpy,即可。
先去部署代码,本来想用fabfile的,考虑到git更能支持pull去同步,所以改用git吧
[root@xxx-general-01 ~]# cd /root/xxx_20180101/web/server/ [root@xxx-general-01 server]# ll total 20 drwxr-xr-x 3 root root 4096 Dec 6 10:21 for_backup_mongodb drwxr-xr-x 2 root root 4096 Jul 25 09:53 for_bacup_mysql drwxr-xr-x 8 root root 4096 Dec 13 10:23 xxx srwxrwxrwx 1 root root 0 Dec 13 14:51 xxx.sock drwxr-xr-x 10 root root 4096 Dec 13 14:47 robotDemo drwxr-xr-x 10 root root 4096 Dec 5 11:14 StorybookQueryServer [root@xxx-general-01 server]# git clone https://gitee.com/xxx_tech/xxx.git Cloning into 'xxx'... remote: Enumerating objects: 60, done. remote: Counting objects: 100% (60/60), done. remote: Compressing objects: 100% (57/57), done. remote: Total 60 (delta 18), reused 0 (delta 0) Unpacking objects: 100% (60/60), done. [root@xxx-general-01 server]# ll total 24 drwxr-xr-x 7 root root 4096 Dec 21 16:44 xxx drwxr-xr-x 3 root root 4096 Dec 6 10:21 for_backup_mongodb drwxr-xr-x 2 root root 4096 Jul 25 09:53 for_bacup_mysql drwxr-xr-x 8 root root 4096 Dec 13 10:23 xxx srwxrwxrwx 1 root root 0 Dec 13 14:51 xxx.sock drwxr-xr-x 10 root root 4096 Dec 13 14:47 robotDemo drwxr-xr-x 10 root root 4096 Dec 5 11:14 StorybookQueryServer [root@xxx-general-01 server]# cd xxx/ [root@xxx-general-01 xxx]# ll total 36 -rw-r--r-- 1 root root 1408 Dec 21 16:44 app.py drwxr-xr-x 2 root root 4096 Dec 21 16:44 common drwxr-xr-x 5 root root 4096 Dec 21 16:44 conf drwxr-xr-x 2 root root 4096 Dec 21 16:44 deploy -rw-r--r-- 1 root root 4637 Dec 21 16:44 factory.py -rw-r--r-- 1 root root 374 Dec 21 16:44 Pipfile -rw-r--r-- 1 root root 2556 Dec 21 16:44 README.md drwxr-xr-x 2 root root 4096 Dec 21 16:44 resources [root@xxx-general-01 xxx]# git status # On branch master nothing to commit, working directory clean
然后去:pipenv install
[root@xxx-general-01 xxx]# pipenv install Creating a virtualenv for this project… Using /root/miniconda3/bin/python3.6m (3.6.5) to create virtualenv… ⠋Running virtualenv with interpreter /root/miniconda3/bin/python3.6m Using base prefix '/root/miniconda3' New python executable in /root/.local/share/virtualenvs/xxx-on-MIKE7/bin/python3.6m Also creating executable in /root/.local/share/virtualenvs/xxx-on-MIKE7/bin/python Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file. Installing setuptools, pip, wheel...done. Virtualenv location: /root/.local/share/virtualenvs/xxx-on-MIKE7 Pipfile.lock not found, creating… Locking [dev-packages] dependencies… Locking [packages] dependencies… Updated Pipfile.lock (d06c53)! Installing dependencies from Pipfile.lock (d06c53)… ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 18/18 — 00:00:07 To activate this project's virtualenv, run the following: $ pipenv shell
记录下安装的库的版本:
[root@xxx-general-01 xxx_20180101]# cd web/server/xxx/ [root@xxx-general-01 xxx]# git status # On branch master nothing to commit, working directory clean [root@xxx-general-01 xxx]# pipenv graph Flask-Cors==3.0.7 - Flask [required: >=0.9, installed: 1.0.2] - click [required: >=5.1, installed: 7.0] - itsdangerous [required: >=0.24, installed: 1.1.0] - Jinja2 [required: >=2.10, installed: 2.10] - MarkupSafe [required: >=0.23, installed: 1.1.0] - Werkzeug [required: >=0.14, installed: 0.14.1] - Six [required: Any, installed: 1.12.0] Flask-PyMongo==2.2.0 - Flask [required: >=0.11, installed: 1.0.2] - click [required: >=5.1, installed: 7.0] - itsdangerous [required: >=0.24, installed: 1.1.0] - Jinja2 [required: >=2.10, installed: 2.10] - MarkupSafe [required: >=0.23, installed: 1.1.0] - Werkzeug [required: >=0.14, installed: 0.14.1] - PyMongo [required: >=3.0, installed: 3.7.2] Flask-RESTful==0.3.7 - aniso8601 [required: >=0.82, installed: 4.0.1] - Flask [required: >=0.8, installed: 1.0.2] - click [required: >=5.1, installed: 7.0] - itsdangerous [required: >=0.24, installed: 1.1.0] - Jinja2 [required: >=2.10, installed: 2.10] - MarkupSafe [required: >=0.23, installed: 1.1.0] - Werkzeug [required: >=0.14, installed: 0.14.1] - pytz [required: Any, installed: 2018.7] - six [required: >=1.3.0, installed: 1.12.0] gevent==1.3.7 - greenlet [required: >=0.4.14, installed: 0.4.15] gunicorn==19.9.0 numpy==1.15.4 python-dotenv==0.10.1
不过运行服务器代码之前,先去确认此处的
supervisor的配置
以及 MongoDB能否访问数据:确认了,在线是有数据了:
另外,对于此处Flask后台所依赖的Evaluation的类,虽然更新了:
但是竟然路径都是相对路径,估计合并会有问题。所以暂时放弃,抽空再去试试。
现用自己的模拟的。
那再去弄supervisor的配置:
【已解决】部署Flask时运行supervisorctl出错:unix:///var/run/supervisor/supervisor.sock no such file
再去设置阿里云安全组,添加允许端口访问:
然后此处可以访问api了:
然后接着去弄https的事情。
{{evalation_host}}/evaluation?eval_id=5c1cbcb8dc5486203c51ab11&action=nextQuestion
返回:
{ "code": 200, "data": { "progressBar": 0.4, "question": { "_id": "5c1777e1cc6df4563adf4c68", "audio": "http://xxx:34800/audio/5c1c9cb312758830e5481f3e/3569.mp3", "audio_length": 0, "audio_text": " look book bike ", "ave_answer_time": 15, "checkpoint": [ "listening", "phonics", "分辨字母和字母组合在单词中的发音", "", "" ], "difficulty": 2.1, "major_type": "单选", "max_answer_time": 30, "question_number": 538, "stem_image": "http://xxx:34800/image/5c1c9cbc12758830e5482666/look.png", "stem_text": "选择押韵的单词", "stem_type": "mix", "sub_questions": [ { "correct_option": [ 1 ], "option_type": "image", "options": [ { "option_image": "http://xxx:34800/image/5c1c9cb812758830e54822ca/book.png", "option_index": 1, "option_text": "" }, { "option_image": "http://xxx:34800/image/5c1c9cb512758830e5481f41/bike.png", "option_index": 2, "option_text": "" } ], "question_texts": [ "" ] } ] }, "skipAllowed": true }, "message": "Get next question ok" }
接着去访问音频和图片,也都可以正常下载的:
接着就是去弄https,使得小程序
【已解决】CentOS更新nginx配置使得小程序能访问测评系统的Flask后台接口
那再去postman中,添加新的环境变量https的,也可以访问接口了:
再去小程序中换成最新https接口,继续去:
【记录】用小程序实现测评系统的前端页面
转载请注明:在路上 » 【已解决】部署测评系统Flask后台到在线服务器