[mod_python] cgihandler and redirects

Andrew Lientz lientz at harvestsolutions.com
Fri Mar 21 18:06:26 EST 2003


We are going to slowly convert to mod_python for our application.  In
order to start the transitions I had to correct three things to allow
the cgihandler.py to make our application work properly.  

I edited apache.py to look for the Location header.  When it finds it it
sets the status. I had a problem with the status raised in cgihandler.py
being overridden.  I ended up getting the nicely formated OK pages out
of the web server.  I added the code below to the write function in the
CGIStdout class.

Here's the diff output of my changes:
<                     if h.lower() == "location":
<                         self.req.status = HTTP_MOVED_TEMPORARILY

This allows the code below to be read and redirected without the OK

print "Location: http://blah\n"

and not need any special changes.

In cgihandler.py, I had to append the the current directory to
sys.path.  If it's a subdirectory of the main directory, the
subdirectory doesn't get passed.  This messes with import commands
within a script.  

Also, sys.exit(0) is an easy way to end a script.  It should be graceful
but it raises a SystemExit error.  I modified the code to deal with

Here's the diff output of what I changed in cgihandler.py.

<             sys.path.append (dir)
<             # this executes the module
<             try:
<                 imp.load_module(module_name, fd, path, desc)
<             except SystemExit, e:
<                 if e == 0:
<                     pass
<                 else:
<                     raise SystemExit, e

I am posting this as an FYI and to see if there was something better I
could have done.  I couldn't find any references in the list archives,
so I hope this helps.


Andrew Lientz
Harvest Solutions, Inc.

More information about the Mod_python mailing list