Python Web development basal procedure

AgendaPipVirtualenvFlaskGunicProcedureSetp 1 Install Pip

Install Pip for package management.Pip is a popular and effective tool for package management.

$ easy_install pip

To access the best practice, you can specify the version of easy_install like “easy_install-2.6 pip”, since our system has included various python interpreter versions.

Setp 2 Install Virualenv

Install Virtualenv by pip.To avoid our system env being contaminated by the current project.

$ pip install virtualenv

Then you can create an virtual environment for the current project.

$ virtualenv .env   

Now, you must activate this virtual environment to use custom python interpreters.

$ source .env/bin/activate(.env)$ 

When you finish your code. you can deactivate virtual env to back to original python interpreters.

(.env)$ deactivate$ 

Step 3 Install Flask

Install Flask by pip.Flask is a micro web development framework for Python. It’s effective and not too bloat like django.shell$ pip install flask

Let’s create a simple web page.

(.env)$ cat >> main.py << EOF> from flask import Flask> app = Flask(__name__)> > @app.route("/")> def hello():> return "Hello World!"> > if __name__ == "__main__":> app.run()> EOF(.env)$ python main.py * Running on http://127.0.0.1:5000/

Now, you can view this page at http://127.0.0.1:5000/.

Step 4 Install Gunicorn

The development of web server is always a tricky issue in web development. Fortunately, in python there are a few tools including Gunicorn to help you finish it efficiently.

(.env)$ pip install gunicorn(.env)$ gunicorn main:app2013-01-05 14:52:41 [84334] [INFO] Starting unicorn 0.16.12013-01-05 14:52:41 [84334] [INFO] Listening at: http://127.0.0.1:8000 (84334)2013-01-05 14:52:41 [84334] [INFO] Using worker: sync2013-01-05 14:52:41 [84335] [INFO] Booting worker with pid: 84335

Conclusion

The complexity of web development builds it a high threshold. Even a practice like “Hello World” has involved network, http protocol, browser, HTML etc.These frameworks and tools can let us focus on logic layouts, finishing our missions quickly and efficiently.

PipVirtualenvFlaskGunicorn

Python Web development basal procedure

相关文章:

你感兴趣的文章:

标签云: