[mod_python] Photo Album Viewer and req.path_into.

Graham Dumpleton grahamd at dscpl.com.au
Fri Feb 4 22:51:32 EST 2005

On 05/02/2005, at 1:27 AM, Shawn Harrison wrote:

>> I could mention Vampire yet again and how it has solved this problem
>> and many other module loading issues, but based on the continuing
>> followups which show that no one has read what I said, I will refrain
>> from doing so ..... ;-(
> I do have a question about Vampire. I want to have a Python script
> 	/web/websitename/www/photos/index.py
> where I've appended /web to sys.path, so that
> 	websitename.www.photos.index
> is a module name. Okay. I want to publish photos with urls like
> 	http://websitename/photos/2004/10/01/137
> Can Vampire send all requests for paths under /photos/ to the photos

Not knowing for sure what you are wanting to do, I am going to assume 
you want to wrap image files in a HTML page automatically.

Firstly, Vampire is structured so as to avoid altogether use of sys.path
and all the complications that can arise because of it. Thus, you would
not add "/web" to sys.path. Thus, to configure things, one would use:

   Options -Indexes -MultiViews

   SetHandler python-program
   PythonHandler vampire
   PythonPath 'sys.path'
   PythonDebug On

Indexes are turned off because we wouldn't want it, and MultiViews is 
off because auto negotiation of file types screws with how Vampire 

PythonPath is set to "sys.path" in order to stop mod_python from adding 
directory into "sys.path" as we don't want that either. This is so 
tries to attempt to load any Python code files in the document tree as 
modules using "import".

Assuming that the "photos" directory is where the images actually live, 
going to change things around a little and rename "photos" to "library".

Rather than create a Python code file residing at "photos/index.py" it 
be called "photos.py". In this is placed the code at the end of this 

I can now say:


What will happen with how I have written this is that it will display a 
page including a header which is the path to the image. It will include 
include an <img> tag so that the image will be loaded into the HTML 
page as

In terms of what you are probably wanting, the important thing is the
"req.path_info" data. This is the part of the path which matches beyond 
actual content handler found to service the request. This was used to 
in the "library" directory to see if there was a corresponding image 
a supported extension type.

If none was found, a not found error would be returned. If one was, the
HTML page would be displayed.

One could if one wants, really fancy up the HTML page and also detect 
reference was made to a directory and generate a HTML index page 
With the help of other content handler scripts, one could even generate
thumbnails on the fly in the index pages where appropriate.

Anyway, not sure how close this might be to what you want to finally
achieve, but at least shows use of "req.path_info".

Note that the reason I had to rename the directory containing the 
images is
that the way Apache resolves paths, it will give precedence to 
search matching and thus will not hand off control to Vampire properly 
if I
had "photos.py" handler and "photos" directory.

I could have instead put the code in the "photos" subdirectory, but 
have called it "index.py" but "browse.py". This is nothing to do with 
problems that mod_python.publisher has with having lots of files called
"index.py", as Vampire can quite happily cope with that. It is because 
URL would then needed to have instead be:


It looks better to have "browse" rather than "index". Note though that 
I had called it "browse.py" and put it in the directory itself, the code
would need a slight tweak and where "subdir" is set to "library", make 
it "."
instead. Ie., start searching from the current directory and not a 

Hope this is of interest. This might actually be a good example for me 
showing off Vampire. Although, I could see it being extended further 
this simple bit of code to become a nice photo album viewer.


# Now for the code.

from mod_python import apache

import os
import posixpath

_PAGE = """
<h1>Photo: %(name)s</h1>
<img src="%(url)s" />

# Handler for virtual directory.

def handler(req):

   # Determine stub of image file to view.

   if req.path_info == "":
     return apache.HTTP_BAD_REQUEST
   elif req.path_info == "/":
     return apache.HTTP_BAD_REQUEST

   name = os.path.splitext(req.path_info[1:])[0]

   # Determine what type of image file it is.

   subdir = "library"
   # subdir = "."  # use this if wanting to make it "browse.py" in 
"photos" subdir

   root = os.path.join(os.path.dirname(req.filename),subdir)

   path = None
   extn = None

   allowed = [ ".jpg", ".gif" ]

   for suffix in allowed:
     file = os.path.join(root,name) + suffix

     if os.path.exists(file):
       path = file
       extn = suffix

   if path == None:
     return apache.HTTP_NOT_FOUND

   # Determine the URL to the actual image.

   baseurl = os.path.dirname(req.uri[:-len(req.path_info)])

   url = posixpath.join(baseurl,subdir,name+extn)

   content = _PAGE % { "name": name, "url": url }

   # Return the rendered page content.

   req.content_type = "text/html"


   return apache.OK

More information about the Mod_python mailing list