[mod_python] How do I serve up .html files?

Conrad Steenberg conrad at hep.caltech.edu
Mon Aug 25 14:42:04 EST 2003

Hi Russel

The way I do it is to have a handler that reposonds to GET requests. 
E.g. if you have a documentroot of /var/www/html:

<Directory /var/www/html>
       SetHandler python-program
       AddHandler python-program .py
       PythonDebug On
       PythonHandler my_handler
       DirectoryIndex myhandler.py

I.e. myhandler.py is the directory index. Then inside the handler
function in myhandler.py:

def handler(req):
  if req.method=='GET':
     # Get the name of the file requested
     inputname=reduce(lambda x,y:x+'/'+y,string.split(req.uri, '/')[1:])

     # Construct an outputname
     # Send the file to the client
     send_file(req,outputname,0,-1) # Or your own function doing lots of req.write()'s
     return apache.OK

This is pseudo-code taken from another implmentation, so play around
with the inputname statement to get it right. But the idea is to get the
filename from req.uri, and then construct a real filesystem filename,
the contents of which can be sent to the client.

An implementation of the send_file() function above is attached, making
use of the req.write_file() function if available (I think it is in the
CVS version), or a slower version based on req.write()



On Mon, 2003-08-25 at 13:54, Russell Nelson wrote:
> Maybe this is really an Apache question, but it seems to me like it
> ought to come up in a mod_python context as well.  Can't find it in
> the documentation, the FAQ, or the mailing list archives
> (Aug/Jul/Jun).
> How do I cause my python script to be run when a .html file is accessed?
>     http://angry-economics.russnelson.com/index.html
> How do I cause my python script to be run when any old file is accessed?
>     http://angry-economics.russnelson.com/
> In other words, I want a handler which gets run no matter what the
> rest of the URL is.  You'd think this would be in the documentation,
> enabling people to make fully scripted sites, but it seems not to be.
> I can't insert a /foo.py/ in there because I have to preserve all the
> old URLs.
Conrad Steenberg <conrad at hep.caltech.edu>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clarens_file.py
Type: text/x-python
Size: 3547 bytes
Desc: not available
Url : http://mailman.modpython.org/pipermail/mod_python/attachments/20030825/886b9e84/clarens_file-0003.py

More information about the Mod_python mailing list