Get Flask App on Heroku
看完了Heroku基本教程后还是不清楚该如何通过Flask使用;在网上找到一个教程
现将Chap4中的所有文件复制到chap6下,以此为基础
- 创建
Procfile
文件touch Procfile
- 将以下信息填写入
Procfile
文件web: gunicorn app:app
- 创建
requirements.txt
文件touch requirements.txt
- 将目前的版本输入到
requirements.txt
中pip freeze > requirements.txt
安装
gunicorn
并加入requirements.txt
$ pip install gunicore Collecting gunicorn Using cached gunicorn-19.6.0-py2.py3-none-any.whl Installing collected packages: gunicorn Successfully installed gunicorn-19.6.0 $ pip freeze > requirements.txt
创建
runtime.txt
并加入当前Python版本python-3.6.0
Commit change in git (也可以push到github中)
- 创建两个新的Heroku Apps
- one for production
$ heroku create weatherbyzephyr-pro Creating ⬢ weatherbyzephyr-pro... done https://weatherbyzephyr-pro.herokuapp.com/ | https://git.heroku.com/weatherbyzephyr-pro.git
- one for staging
$ heroku create weatherbyzephyr-stage Creating ⬢ weatherbyzephyr-stage... done https://weatherbyzephyr-stage.herokuapp.com/ | https://git.heroku.com/weatherbyzephyr-stage.git
- one for production
将创建Apps添加到我的
git remotes
,主要一个命名为pro,另一个为stage$ git remote add pro [email protected]:weatherbyzephyr-pro.git $ git remote add stage [email protected]:weatherbyzephyr-stage.git
- Push apps live to heroku
$ git push stage master $ git push pro master
弹错,无权限;
heroku login
后再次尝试还是不行Warning: Permanently added the RSA host key for IP address '50.19.85.156' to the list of known hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
在stackoverflow上找到这个问题的答案,与SSH key 有关; 另写一个教程回答,这里略过。
(venv) Zephyr (master) project $ git push stage master Counting objects: 2055, done. Delta compression using up to 4 threads. Compressing objects: 100% (2022/2022), done. packet_write_wait: Connection to 50.19.85.154: Broken pipe Writing objects: 100% (2055/2055), 10.92 MiB | 31.00 KiB/s, done. Total 2055 (delta 337), reused 0 (delta 0) remote: Compressing source files... done. remote: Building source: remote: remote: -----> Python app detected remote: -----> Installing python-3.6.0 remote: $ pip install -r requirements.txt remote: Collecting appdirs==1.4.0 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 1)) remote: Downloading appdirs-1.4.0-py2.py3-none-any.whl remote: Collecting click==6.7 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 2)) remote: Downloading click-6.7-py2.py3-none-any.whl (71kB) remote: Collecting Flask==0.12 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 3)) remote: Downloading Flask-0.12-py2.py3-none-any.whl (82kB) remote: Collecting gunicorn==19.6.0 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 4)) remote: Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB) remote: Collecting itsdangerous==0.24 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 5)) remote: Downloading itsdangerous-0.24.tar.gz (46kB) remote: Collecting Jinja2==2.9.5 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 6)) remote: Downloading Jinja2-2.9.5-py2.py3-none-any.whl (340kB) remote: Collecting MarkupSafe==0.23 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 7)) remote: Downloading MarkupSafe-0.23.tar.gz remote: Collecting packaging==16.8 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 8)) remote: Downloading packaging-16.8-py2.py3-none-any.whl remote: Collecting pinyin==0.4.0 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 9)) remote: Downloading pinyin-0.4.0.tar.gz (3.6MB) remote: Collecting pyparsing==2.1.10 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 10)) remote: Downloading pyparsing-2.1.10-py2.py3-none-any.whl (56kB) remote: Collecting requests==2.13.0 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 11)) remote: Downloading requests-2.13.0-py2.py3-none-any.whl (584kB) remote: Collecting six==1.10.0 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 12)) remote: Downloading six-1.10.0-py2.py3-none-any.whl remote: Collecting Werkzeug==0.11.15 (from -r /tmp/build_9f7e0f956f0068139df6e65132e52ca5/requirements.txt (line 13)) remote: Downloading Werkzeug-0.11.15-py2.py3-none-any.whl (307kB) remote: Installing collected packages: appdirs, click, itsdangerous, MarkupSafe, Jinja2, Werkzeug, Flask, gunicorn, six, pyparsing, packaging, pinyin, requests remote: Running setup.py install for itsdangerous: started remote: Running setup.py install for itsdangerous: finished with status 'done' remote: Running setup.py install for MarkupSafe: started remote: Running setup.py install for MarkupSafe: finished with status 'done' remote: Running setup.py install for pinyin: started remote: Running setup.py install for pinyin: finished with status 'done' remote: Successfully installed Flask-0.12 Jinja2-2.9.5 MarkupSafe-0.23 Werkzeug-0.11.15 appdirs-1.4.0 click-6.7 gunicorn-19.6.0 itsdangerous-0.24 packaging-16.8 pinyin-0.4.0 pyparsing-2.1.10 requests-2.13.0 six-1.10.0 remote: remote: -----> Discovering process types remote: Procfile declares types -> web remote: remote: -----> Compressing... remote: Done: 75.2M remote: -----> Launching... remote: Released v3 remote: https://weatherbyzephyr-stage.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy.... done. To heroku.com:weatherbyzephyr-stage.git * [new branch] master -> master
出现问题的
部署OK了,界面也在;但是外网API无法查询; 试错了好久
发现是r = requests.get(base_url, params=params,timeout=1)
的问题,应该是heroku服务器访问有延时,导致结果搜索不出来;去掉timeout
参数后便正常了
Notes
flask debug 模式
if __name__ == "__main__":
app.debug = True
app.run()
Chage Log
2017.02.25 Initial Draft