mike watkins dot ca : Entries tagged with “Wsgi”

Entries tagged with “Wsgi”

January 21 2009

QP and WSGI

QP doesn't use WSGI itself in the operation of its own built-in web and SCGI servers, but the framework does make it easy to drive QP applications via WSGI if you have a need to. Here's a quick how-to.

Exposing the QP application as a WSGI app is simple. Create a driver file (let's name it runwsgi.py) and place it in your Python path. Myself I prefer to put such files in my QP application's "site" directory.

# this is runwsgi.py
from qp.lib.site import Site
site = Site('songs')
application = site.get_publisher()

You can then run a WSGI server (such as Spawning) from the command line:

% spawn -t 0 -p 8000 qp.sites.songs.runwsgi.application

Or we can extend runwsgi.py to also provide a self-contained WSGI server:

#! /usr/bin/env python
# this is runwsgi.py
from qp.lib.site import Site
site = Site('songs')
application = site.get_publisher()

if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    host, port = site.get_http_address()
    httpd = make_server(host, port, application)
    hp = httpd.socket.getsockname()
    print("Serving HTTP on %s port %d..." % (hp[0], hp[1]))
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("\nStopping server(s)")
        if site.is_durus_running():
            site.stop_durus()

We can execute this directly; the application will use the same host and port defined in our application's configuration.

% ./runwsgi.py

Python 3 Compatibility

In keeping with my blog's objective of only publishing examples that are compatible with both Python 2.x and 3.x, I need to point out that the QP code presented certainly will run on Python 3.0 but the wsgi servers (spawning and the reference WSGI server) noted will not.

While QP and the rest of its stack is compatible with Python >= 2.4 (yes, that means Python 3.x too), the reference implementation of WSGI supplied with Python 3.0 is broken. As far as I know the only working WSGI server implementation for Python 3 at present is mod_wsgi, available for Apache here.