paulgorman.org

Python Web Development

WSGI

WSGI is the Python Web Server Gateway Interface, an interface between a webserver and a web framework. Like CGI, WSGI is a protocol. Many webservers, including Apache and nginx, support WSGI. This means we don't have to worry about different web servers when writing our application (and developers of different frameworks don't have to worry about particular web servers either).

Gunicorn is a nice little WSGI web server to use for development. It's even installable into our virtualenvs with pip, like: virtualenv myproject; cd myproject; source bin/activate; pip install gunicorn. However, some frameworks, including Bottle, come with their own testing/development WSGI servers, so don't rush to install Gunicorn. Something like Apache's mod_wsgi may be more suitable for production use.

Create test.py:

def hello(environ, start_response):
    """Very simple WSGI demo."""
    msg = b'Hello, world!\n'
    status = '200 OK'
    response_headers = [
        ('Content-type','text/plain'),
        ('Content-Length', str(len(msg)))
    ]
    start_response(status, response_headers)
    return iter([msg])

Fire up the WSGI server, like $ gunicorn -b localhost:8888 test:hello, and hit http://localhost:8888.

pip and virtualenv

sudo apt-get install python-pip python-virtualenv

pip is a package manager for Python, like CPAN for Perl or gems for Ruby.

Don't install packages globally on a system (e.g. — don't `sudo pip whatever`). Instead, use virtualenv.

virtualenv lets us install pip packages in an isolated directory , just for our project, so that packages and versions don't conflict like they would installed globally on a system. It also make packaging our project for distribution easier.

$ cd ~/repo/
$ virtualenv myproject
New python executable in myproject/bin/python
Installing setuptools............done.
Installing pip...............done.

Activate the virtual environment, and install packages:

$ cd ~/repo/myproject/
$ source bin/activate
<myproject>$ pip install bottle

When we're done working in the virtual environment:

<myproject>$ deactivate
$ 

<myproject>$ pip freeze > requirements.txt creates a file showing all the packages and package versions in the environment, which will be useful if we (or another user or developer) want to recreate/deploy the app.

.git

When we set up our .git repo in myproject/, we'll probably want to include the virtualenv directories in .gitignore:

bin/
include/
lib/
local/ 

Frameworks

Django and Flask are two of Pythons's many web frameworks. Django is a batteries-included framework; Flask is light-weight.

We'll be looking at the framework Bottle, which is similar to Flask.

from bottle import route, run, template

@route('/hello')
def hello():
    return "Hello, World!"
@route('/hello/<name>')
def helloName(name):
    return template('Hello, {{name}}, how are you?', name=name)

run(host='localhost', port=8080, debug=True)

We run it like: python ./hello.py, and point our web browser at http://127.0.0.1:8080/hello or http://127.0.0.1:8080/hello/Paul.

The Bottle tutorial is surprisingly thorough, and the whole framework is a single file with under 4,000 lines of source code.

References