[mod_python] handling virtual files

Graham Dumpleton grahamd at dscpl.com.au
Sat Jul 16 20:55:37 EDT 2005

On 17/07/2005, at 12:07 AM, Alejandro Mery wrote:

>> What base handler were you thinking of using, were you thinking
>> of starting with something like mod_python.publisher or writing
>> your own handlers from scratch?
> my own PythonHandler-s from scratch. I want to write handlers for the  
> different kind of folders and for the different kind of file, but  
> keeping file handlers also usable as direct handlers. for example a  
> handler to render dir/foo.xmi as png and dir/foo.xmi?height=200 with  
> that fixed height and scaling. or html from .tex files, with  
> foo.txt?pdf=yes&papersize=letter options even if files are real or  
> not.
> i mean, to be called by folder handler or directly by apache.

If your virtual file is notionally adjacent to an actual physical file,
the quickest way to get something working may be to use Vampire. This
is because Vampire was designed to make this specific sort of thing
very easy.

In Vampire it does dispatching to a Python module associated with a
physical file and can then within that Python module select a handler
based on the extension to the request.

Consider the ".tex" case first. Here you would have "foo.tex" present
and next to it you would create a "foo.py" Python module. In it you  

   def handler_html(req):
     # render req.filename as HTML

   def handler_pdf(req,papersize="letter"):
     # render req.filename as PDF

You could have a customised Python module associated with each ".tex"  
or, because the process could be the same for all ".tex" files in a  
you could specify a default handler for ".html" or ".pdf" which is  
for any matching request against the directory and it would process the  
file appropriately.

For an actual example, see:


In this case rather than a ".tex" file as input, it is a ".rml" file
with output being ".pdf".

For another example of this idea of multiple views of a physical
resource, see:


Here there is a physical ".csv" file, but also virtual resource  
handlers in
the associated Python module for returning the data as HTML or tab  

Note how the Python module even has a handler for a ".csv" request so  
some checking can be done, with req.sendfile() being used to return the
actual ".csv" data.

You could do something similar for your ".xmi" files, although, it may  
sense to have a handler for ".png" which transforms ".xmi" file to PNG  
sends the response. Ie., use the appropriate extension type for the top
of content being returned.

> i have nothing serious written or designed yet, if you think i need  
> WSGI lets go there :)

Based on how you describe what you want, you might try Vampire first.  
complicated things can still be done with Vampire, but depends on how  
want that to work.

BTW, Vampire is available from:


This is not the volatile package I talked of, that is something  
different I
am working on. Vampire is stable, with only known issue being an obscure
thing related to module reloading which you unlikely to run up against  

There are various examples included with the source code, including all  


More information about the Mod_python mailing list