[mod_python] how do I eliminate the .py extension in url?

Shawn Harrison harrison at tbc.net
Thu Feb 10 19:30:32 EST 2005


Too quick on the send key. Here's the file.

Jef Dodson wrote [02/10/05 5:39 PM]:
> Also, it's not a very general solution.  I would like to be able to access any number of scripts
> without having the .py show up in the URL.  I am sure it is possible because I had it working
> under Linux, but I am now trying to get it working under BSD so I'm wondering if maybe there is
> some option that I'm missing in my httpd config file that was there by default on the Linux
> installation.  Anyone have any ideas on this?

Put the following in httpd.conf:

SetHandler mod_python
PythonHandler module.path.to.handler.file

Then in the handler file, def a handler that looks for python scripts
and loads them based on the req.uri. The attached very crufty handler
might be a good source of amusement.

-- 
________________
harrison at tbc.net


-------------- next part --------------

# there's some stuff here that the handler doesn't actually use....
from mod_python import apache, psp, util, Cookie, Session
from string import split, join
import os

def handler(req):
    # 1. If the file exists & not a directory, serve it.

    filepath = req.document_root() + req.uri
    if (os.access(filepath, os.R_OK) 
    and not os.path.isdir(filepath)) :
        # req.write("sending file\n")
        req.sendfile(filepath)
        return apache.OK

    # 2. Search for a PSP or PY file to handle the request
    # -- working backwards from the end of the URI

    urllist = split(req.uri, '/')

    thefile = None
    for i in range(len(urllist)):
        filepath = os.path.join(req.document_root(), 
                join(urllist[1:len(urllist)-i], '/'))

        files = ('', 'index')             # keep lists short! only what is used
        exts = ('psp', 'py')              # each item adds to the processing time.

        for j in range(len(files)):
            for k in range(len(exts)):
                if files[j] == '':
                    thefile = ("%s.%s"
                              % (filepath, exts[k]))
                else:
                    thefile = ("%s.%s" 
                               % (os.path.join(filepath, files[j]), exts[k]))
                # DEBUG
                #req.content_type = "text/html"
                #req.write("%s<br />\n" % thefile)

                if os.access(thefile, os.R_OK):
                    if exts[k] == 'py':    
                        # there are better ways to do this....
                        # assumes thefile is a script that can be "run" directly.
                        execfile(thefile)    # replace w/import?
                    elif exts[k] == 'psp':
                        template = psp.PSP(req, filename=thefile)
                        template.run({'psp_file':thefile})
                    else:
                        # default is to treat the file as HTML. 
                        req.content_type="text/html"
                        req.sendfile(thefile)

                    return apache.OK

    # 3. Give up: Nothing found.

    return apache.HTTP_NOT_FOUND




More information about the Mod_python mailing list