- 浏览: 96512 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (61)
- Hibernate (5)
- WebService (6)
- Python (13)
- ExtJs (0)
- Java (20)
- SMB (1)
- Game (1)
- Java Advanced Image (1)
- CMD (4)
- Oracle (2)
- Windows (2)
- Linux (1)
- Forums (1)
- Struts (2)
- Internationalization (1)
- NTLM (1)
- HttpClient (1)
- Http (1)
- Form (1)
- Tomcat (2)
- Log4j (1)
- Eclipse (1)
- ant (1)
- soap (0)
- SSL (2)
- security (2)
- permission (1)
- 面试 (0)
- authentication (1)
- Spring (0)
- ioc (0)
- javascript (1)
- license (0)
- web (0)
- Maven (0)
- website (0)
- tool (0)
- git (1)
- Thread (2)
- 软件工程 (0)
- mongodb (1)
最新评论
-
howgoo:
OpenSystemArchitect 中文乱码。
免费的数据库建模工具 -
tojaoomy:
如果需要输出时不换行,在最后加上逗号即可。比如print 'H ...
Python静态属性,静态方法 -
tojaoomy:
http://www.oracle.com/technetwo ...
丢失更新 -
tojaoomy:
teasp 写道tojaoomy 写道teasp 写道toja ...
synchronized (this) 柳暗花明又一村 -
teasp:
tojaoomy 写道teasp 写道tojaoomy 写道t ...
synchronized (this) 柳暗花明又一村
Recently,I have nothing to do ,so i help the Japanese investigate some prototype.
One was post the MFP(Mutli-Fuctional Printer) statue to the Twitter/Facebook.It was boring.
Then i found something interesting follow this.
Getting Started with Python on Heroku/Cedar
Table of Contents
- Prerequisites
- Local Workstation Setup
- Start Flask App Inside a Virtualenv
- Declare Dependencies with Pip
- Declare Process Types With Foreman/Procfile
- Store Your App in Git
- Deploy to Heroku/Cedar
- Shell
- Using A Different WSGI Server
- Running a Worker
- Next Step: Database-driven Apps via Django
This quickstart will get you going with Python and the Flask web framework on the Cedar stack. For Django apps, please see the Django quickstart.
Prerequisites
- Basic Python knowledge, including installed versions of Python, Virtualenv, and Pip.
- Your application must run on Python v2.7.
- Your application must use Pip to manage dependencies.
- A Heroku user account. Signup is free and instant.
Local Workstation Setup
Install the Heroku Toolbelt on your local workstation. This ensures that you have access to the Heroku command-line client, Foreman, and the Git revision control system.
Once installed, you can use the heroku
command from your command shell. Log in using the email address and password you used when creating your Heroku account:
$ heroku login Enter your Heroku credentials. Email: adam@example.com Password: Could not find an existing public key. Would you like to generate one? [Yn] Generating new SSH public key. Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Press enter at the prompt to upload your existing ssh
key or create a new one, used for pushing code later on.
Start Flask App Inside a Virtualenv
You may be starting from an existing Python app. If not, here’s a simple “hello, world” application you can use.
Start by making an empty top-level directory for the project:
$ mkdir helloflask && cd helloflask
Create a Virtualenv (v0.7):
$ virtualenv venv --distribute New python executable in venv/bin/python Installing distribute...............done. Installing pip...............done.
To activate the new environment, you’ll need to source it:
Windows users can run venv\Scripts\activate.bat
for the same effect.
$ source venv/bin/activate
This will change your prompt to include the project name. (You must source the virtualenv environment for each terminal session where you wish to run your app.)
Install dependencies with pip:
$ pip install flask Downloading/unpacking flask Downloading Flask-0.8.tar.gz (494Kb): 494Kb downloaded Running setup.py egg_info for package flask Downloading/unpacking Werkzeug>=0.6.1 (from flask) Downloading Werkzeug-0.8.2.tar.gz (1.1Mb): 1.1Mb downloaded Running setup.py egg_info for package Werkzeug Downloading/unpacking Jinja2>=2.4 (from flask) Downloading Jinja2-2.6.tar.gz (389Kb): 389Kb downloaded Running setup.py egg_info for package Jinja2 Installing collected packages: flask, Werkzeug, Jinja2 Running setup.py install for flask Running setup.py install for Werkzeug Running setup.py install for Jinja2 Successfully installed flask Werkzeug Jinja2 Cleaning up...
app.py
Now we have a clean Flask environment setup. Let’s create our application, app.py
:
import os from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World!' if __name__ == '__main__': # Bind to PORT if defined, otherwise default to 5000. port = int(os.environ.get('PORT', 5000)) app.run(host='0.0.0.0', port=port)
Declare Dependencies with Pip
Cedar recognizes Python apps by the existence of a requirements.txt
.
Create a pip requirements file which declares our required Python modules:
$ pip freeze > requirements.txt
Make sure that there aren’t any “editable” modules (lines that start with a -e
) in this list. They should never be used in production.
All packages required should be declared explicitly in requirements.txt
:
$ cat requirements.txt Flask==0.8
Declare Process Types With Foreman/Procfile
To run your web process, you need to declare what command to use. In this case, we simply need to execute our Python program. We’ll use Procfile
to declare how our web process type is run.
Here’s a Procfile
for the sample app we’ve been working on:
web: python app.py
Now that you have a Procfile
, you can start your application with Foreman:
$ foreman start 09:17:46 web.1 | started with pid 80875 09:17:46 web.1 | * Running on http://0.0.0.0:5000/
Your app will come up on port 5000 (as specified in app.py
). Test that it’s working with curl
or a web browser, then Ctrl-C to exit.
Store Your App in Git
Exclude Virtualenv artifacts from source control tracking:
GitHub provides an excellent Python gitignore file that can be installed system-wide.
.gitignore
venv *.pyc
We now have the three major components of our app: dependencies in requirements.txt
, process types in Procfile
, and our application source in app.py
. Let’s put it into Git:
$ git init $ git add . $ git commit -m "init"
Deploy to Heroku/Cedar
Create the app on the Cedar stack:
$ heroku create --stack cedar Creating stark-window-524... done, stack is cedar http://stark-window-524.herokuapp.com/ | git@heroku.com:stark-window-524.git Git remote heroku added
Deploy your code:
$ git push heroku master Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 687 bytes, done. Total 6 (delta 0), reused 0 (delta 0) -----> Heroku receiving push -----> Python app detected -----> Preparing virtualenv version 1.7 New python executable in ./bin/python2.7 Also creating executable in ./bin/python Installing distribute............done. Installing pip...............done. -----> Byte-compiling code -----> Installing dependencies using pip version 1.2.0 Downloading/unpacking Flask==0.8 (from -r requirements.txt (line 1)) ... Successfully installed Flask Werkzeug Jinja2 Cleaning up... -----> Discovering process types Procfile declares types -> web -----> Compiled slug size is 3.5MB -----> Launching... done, v2 http://stark-window-524.herokuapp.com deployed to Heroku To git@heroku.com:stark-window-524.git * [new branch] master -> master
Before looking at the app on the web, we’ll need to scale the web process:
$ heroku ps:scale web=1 Scaling web processes... done, now running 1
Now, let’s check the state of the app’s processes:
$ heroku ps Process State Command ------------ ------------------ ------------------------------ web.1 up for 10s python app.py
The web process is up. Review the logs for more information:
$ heroku logs 2011-08-20T16:33:39+00:00 heroku[slugc]: Slug compilation started 2011-08-20T16:34:07+00:00 heroku[api]: Config add PYTHONUNBUFFERED by craig@example.com 2011-08-20T16:34:07+00:00 heroku[api]: Release v1 created by craig@example.com 2011-08-20T16:34:07+00:00 heroku[api]: Deploy 67b7e54 by craig@example.com 2011-08-20T16:34:07+00:00 heroku[api]: Release v2 created by craig@example.com 2011-08-20T16:34:08+00:00 heroku[web.1]: State changed from created to starting 2011-08-20T16:34:08+00:00 heroku[slugc]: Slug compilation finished 2011-08-20T16:34:10+00:00 heroku[web.1]: Starting process with command `python app.py` 2011-08-20T16:34:10+00:00 app[web.1]: * Running on http://0.0.0.0:17658/ 2011-08-20T16:34:11+00:00 heroku[web.1]: State changed from starting to up
Looks good. We can now visit the app with heroku open
.
Shell
Cedar allows you to launch a Python shell attached to your local terminal for experimenting in your app’s environment:
$ heroku run python Running python attached to terminal... up, run.1 Python 2.7.1 (r271:86832, Jun 26 2011, 01:08:11) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
From here you can import
some of your application modules.
Using A Different WSGI Server
The examples above used the default HTTP server for Flask. For production apps, you may wish to use a more production-ready embedded webserver, such as Tornado, gevent’s WSGI server, or Gunicorn.
Let’s try adding Gunicorn to our Flask app by adding the gunicorn
dependency to our Pip file, and changing what command used to run the web process in Procfile
:
requirements.txt
Flask==0.8 gunicorn==0.13.4
Procfile
web: gunicorn app:app -b 0.0.0.0:$PORT -w 3
Update dependencies:
$ pip install -r requirements.txt Requirement already satisfied (use --upgrade to upgrade): Flask==0.8 in ./venv/lib/python2.7/site-packages (from -r requirements.txt (line 1)) Downloading/unpacking gunicorn==0.13.4 (from -r requirements.txt (line 2)) ... Successfully installed gunicorn Cleaning up...
Run locally with Foreman:
$ foreman start 23:57:27 web.1 | started with pid 87365 23:57:27 web.1 | 2011-08-26 23:57:27 [87366] [INFO] Starting gunicorn 0.13.4 23:57:27 web.1 | 2011-08-26 23:57:27 [87366] [INFO] Listening at: http://0.0.0.0:5000 (87366) 23:57:27 web.1 | 2011-08-26 23:57:27 [87366] [INFO] Using worker: sync 23:57:27 web.1 | 2011-08-26 23:57:27 [87367] [INFO] Booting worker with pid: 87367
Deploy:
$ git commit -am "use gunicorn" $ git push heroku master
Running a Worker
The Procfile
format lets you run any number of different process types. For example, let’s say you wanted a worker process to complement your web process:
Procfile
web: python app.py worker: python worker.py
Running more than one dyno for an extended period may incur charges to your account. Read more about dyno-hour costs.
Push this change to Heroku, then launch a worker:
$ heroku ps:scale worker=1 Scaling worker processes... done, now running 1
Check heroku ps
to see that your worker comes up, and heroku logs
to see your worker doing its work.
Next Step: Database-driven Apps via Django
The Django tutorial will guide you through setting up a database-driven Django app on Heroku.
The Reference:
https://devcenter.heroku.com/articles/python
发表评论
-
Getting Started with Python on Heroku/Cedar
2012-04-20 15:18 1Recently,I have nothing to do , ... -
Python调用WebService出错,求解决
2012-03-01 21:43 1435import logginglogging.basicConf ... -
windows下安装suds一些问题
2012-03-01 11:26 4900使用java访问SharePoint,当初不知为何,多种方式失 ... -
Web Service for Python
2012-02-29 15:04 0Welcome to the Python Web Ser ... -
Python图形图像处理库的介绍之Image模块
2012-02-20 17:18 1473Image模块的介绍 ... -
如何创建字典和给字典赋值
2012-02-07 09:39 1168创建字典只需要把字典赋值给一个变量,不管这个字典是否包含元素: ... -
拷贝Python 对象
2012-02-06 17:31 639说当你创建一个对象, ... -
class Foo:pass 与class Foo(object):pass的区别
2012-02-03 12:07 1362>>> class Foo:pass > ... -
str()和 repr() (及 `` 运算符)
2012-02-03 11:16 703内建函数 str() 和 repr() ... -
id()使用,小整数及字符串缓存
2012-02-03 11:11 822整数对象和字符串对象是不可变对象,所以Python 会很高效的 ... -
Sorting Basics
2012-02-02 10:26 626Python lists have a built-in so ... -
Python静态属性,静态方法
2012-02-01 17:06 3783如何定义类 class ClassName(base_cla ... -
python try语句如何打印错误行
2012-02-01 16:05 1638python try语句如何打印错误行 ... -
Python 循环
2012-02-01 16:03 732range()函数经常和len()函数一起用于字符串索引。 在 ...
相关推荐
ruby-getting-started, 在Heroku上,使用 ruby 入门 ruby-getting-started一个易于部署的Rails 应用程序,可以轻松部署到 Heroku 。这个应用程序支持在Heroku上开始使用 ruby 文章- check查看。在本地运行确保已经...
$ git clone https://github.com/heroku/python-getting-started.git $ cd python-getting-started $ python3 -m venv getting-started $ pip install -r requirements.txt $ createdb python_getting_started $ ...
heroku-buildpack-go, Heroku Buildpack Go Go 这是官方的Heroku buildpack,用于 。正在启动遵循 https://devcenter.heroku.com/articles/getting-started-with-go 指南。还有一个 H
$ git clone https://github.com/heroku/go-getting-started.git $ cd go-getting-started $ go build -o bin/go-getting-started -v . # or `go build -o bin/go-getting-started.exe -v .` in git bash github....
Flask / Heroku / ProcFile / Requires.txt] Flask Python Heroku DB:示例如何在Heroku上设置Flask-PostgreSQL数据库[Python / Flask / Heroku / PostgreSQL] Flask Python Heroku ML主机:示例如何使用HTML用户...
$ git clone git@github.com:heroku/node-js-getting-started.git # or clone your own fork $ cd node-js-getting-started $ npm install $ npm start 您的应用程序现在应该在上运行。 部署到 Heroku $ heroku ...
这些软件包是使用docker和Heroku提供的heroku/cedar映像构建的。 为了构建您自己的版本库版本,您将需要一个有效的docker命令行。 最快的入门方法是 。 # Build all the packages, dist folder and repository ...
$ git clone https://github.com/heroku/java-getting-started.git $ cd java-getting-started $ mvn install $ heroku local:start 您的应用现在应该在上运行。 如果要使用数据库,请确保您有一个本地.env文件,...
如何使用Python Poetry Buildpack通过从poetry.lock生成requirements.txt和runtime.txt来准备要由Python buildpack(例如heroku/python处理的构建。 要通过Heroku CLI设置使用多个buildpack,请使用buildpacks:add ...
$ git clone https://github.com/heroku/java-getting-started.git $ cd java-getting-started $ mvn install $ foreman start web 您的应用程序现在应该在上运行。 部署到 Heroku $ heroku create $ git push ...
Vagrant Heroku Cedar-14 盒子基于项目: : Ubuntu 14.04 PosgreSQL 9.4 NodeJS 0.10.25(用于 ExecJS) Ruby(目前是 2.1.5) Rubygems(目前是 2.2.2) 图像魔术师Phantomjs(用于 poltergeist gem) 这个框在 ...
$ git clone git@github.com:heroku/ruby-getting-started.git $ cd ruby-getting-started $ bundle install $ bundle exec rake db:create db:migrate $ heroku local 您的应用现在应该在上运行。 部署到Heroku $...
这个名为 "vagrant-heroku-cedar-14-python" 的项目是专为Django开发设计的,它通过Vagrant模拟了Heroku的cedar-14运行时环境,帮助开发者在本地构建与Heroku生产环境相似的开发环境。下面将详细解释这个项目的各个...
$ git clone https://github.com/heroku/python-getting-started.git $ cd python-getting-started $ python3 -m venv getting-started $ pip install -r requirements.txt $ createdb python_getting_started $ ...
$ git clone git@github.com:heroku/node-js-getting-started.git # or clone your own fork$ cd node-js-getting-started$ npm install$ npm start 您的应用程序现在应该在上运行。部署到Heroku $ heroku create$ ...
**Python-DjangoHero在云上使用Heroku搭建一个Django项目最快的方式** 在现代Web开发中,Python的Django框架以其高效、灵活和强大的功能深受开发者喜爱。而Heroku作为一个流行的云端平台,提供了便捷的部署服务,...
$ git clone https://github.com/heroku/python-getting-started.git$ cd python-getting-started$ python3 -m venv getting-started$ pip install -r requirements.txt$ createdb python_getting_started$ python ...
$ git clone https://github.com/heroku/go-getting-started.git$ cd go-getting-started$ go build -o bin/go-getting-started -v . # or `go build -o bin/go-getting-started.exe -v .` in git bashgithub....
$ git clone https://github.com/heroku/python-getting-started.git$ cd python-getting-started$ python3 -m venv getting-started$ pip install -r requirements.txt$ createdb python_getting_started$ python ...