WSGI with mod_python (was: Python, WSGI, legacy web application)

Graham Dumpleton grahamd at dscpl.com.au
Thu Nov 23 16:49:08 EST 2006


Paul Boddie wrote:
> Rob De Almeida wrote:
> > Ben Finney wrote:
> > > I was under the impression that WSGI in mod_python was a rather kludgy
> > > way to do WSGI, but I don't know what the alternatives are. CGI?
> > > Python http server (e.g. CherryPy)? Something else?
> >
> > You can use FastCGI or SCGI too, with Apache, lighttpd or Cherokee.
>
> I think the motivation behind suggesting an Apache solution was that
> you'd be able to migrate the PHP resources you already have running in
> Apache (I assume, since PHP can run in other places these days) to
> mod_python whilst staying within the Apache environment, rather than
> having to maintain a number of different environments at the same time.
> In other words, you'd write your replacement resources using WSGI (or
> whatever) on mod_python (for performance), CGI (for relative
> simplicity), or some other connection technology, and then it'd just be
> a matter of changing the various directives and having Apache figure it
> out.

Correct.

As example, imagine you have written a mod_python handler which itself
interprets how to map a URL to something to implement the URL. This
might map to a WSGI application or to some system of basic mod_python
handlers.

Within the .htaccess file of the directory where all your PHP files
live you could then write:

  PythonHandler myphppagereplacementhandler | .php

At this point nothing will happen, but then one could do the following:

  <Files index.php>
  SetHandler mod_python
  </Files>

For the one page called 'index.php' the mod_python handler would be
called instead of PHP. As a Python equivalent for each PHP page is
written, just need to trigger the mod_python handler to be used by
using the Files directive.

One could also have different handlers for each page and use Apache to
make the selection if wanted to:

  <Files index.php>
  SetHandler mod_python
  PythonHandler myphppagereplacementshandler::index
  </Files>

Now I am sure that some will say it looks messy, but as far as trying
to do a progressive replacement of pages and maintain URLs, it is
probably the quickest way. It should be said that any progressive
migration like this is likely to be a bit messy.

Don't like this, then another way using Apache might be to use
mod_rewrite to remap URLs to new URLs which use Python code. Using
mod_rewrite can be a pain though. Yet another way may be to use
mod_proxy to selectively forward URLs through to a separate back end
web server if you are Apache phobic and want to use a web server
written in pure Python.

Overall, Apache/mod_python has a lot to offer, but from what I have
seen most Python web frameworks simply uses it as a jumping off point
and not much else.

Graham




More information about the Python-list mailing list