[mod_python] Transparently using mod_python to handle images

Gustavo Córdova Avila gustavo.cordova at q-voz.com
Thu Mar 17 09:53:02 EST 2005

Rich McDonough wrote:

> First let me extend my thanks to this group. It is a fountain of 
> useful information.
> I am sure that this question will be easily answered by someone here, 
> in fact there is probably some code snippets around that will do the 
> deed quickly and easily. I am trying to write a handler that will 
> server images from a subdirectory. The goal is to have the handler 
> serve the image and insert a record into a database indicating the 
> referring page, filename, time, etc... It would ideally be a folder 
> within a UserDir, so for this to work the user would have to add the 
> appropriate .htaccess and index.py files added to it. Users would then 
> just add the images they want served to the folder, the handler would 
> do the rest.
> I have been doing this with Zope for a while now but need to move away 
> from zodb due to size issues. Also, my current approach requires the 
> URL to be rather ugly and include an argument (i.e. 
> http://server/folder/image?sparky.jpg). I would love to get away from 
> passing the argument in the URL and simply have users place the real 
> path to the image, then let the handler do the manipulating for me.
> Does anyone have any quick pointers or examples of such an application?
> Thank you in advance,
> Rich

You're welcome :-)

Something like this, perhaps???

    # Do this for all .../imgsrv/ directories.
    <DirectoryMatch "/imgsrv/">
        SetHandler mod_python
        PythonPath "['/location/of/your/script']+sys.path"
        PythonHandler image_server

And then, you place somewhere in your PythonPath:

    # image_server.py
    # This small handler serves images, and records
    # the information to who it was sent to.

    import os, re, time
    from mod_python import apache

    rxParts = re.compile(r"(([^/]+)\.([^./]+))$")

    # The function "record_serving" handles inserting into the
    # database a record of an image served.
    # Usage: RecordServing(
    from DatabaseStuff import RecordServing

    # This is your document root.
    DOCROOT = "/var/www/htdocs"

    def handler(req):
        # *IF* the image exists, then record a serving.
        if os.access(DOCROOT + req.uri, os.F_OK):
           fullname, name, extension = rxParts.search(req.uri).groups()
           referer = req.header_in.get("referer")
           timestamp = time.strftime("%F %T")
           RecordServing(timestamp, fullname, referer)

        # Finally, let Apache send the file.
        return apache.DECLINED

What this handler does, is intercept all fetches for files inside all 
subdirectories called "imgsrv"; when the file actually exists, it saves 
a record to the database (vía the "RecordServing" function). Finally, 
the request is declined, to let apache handle sending the actual file to 
the client.

I haven't actually run it, but the basic design should work.  Comments 
are appreciated.

Hope this helps.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: gustavo.cordova.vcf
Type: text/x-vcard
Size: 189 bytes
Desc: not available
Url : http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20050317/c3abe2c1/gustavo.cordova.vcf

More information about the Mod_python mailing list