[mod_python] Re: QUERY_STRING and running a cgi script under mod_python

Gambit gambit at alpenjodel.de
Mon Jul 23 21:57:29 EDT 2007


Thanks for the reply.

It's an old cgi script someone else wrote. I rewrote it to use 
mod_python style req.form parameters and it works now, so this is mostly 
an academic discussion.

Yes, I'm pretty sure I'm under mod_python, I won't post the server 
configuration here as this server is managed trough the Plesk control 
panel which means the configuration is split in many complex files all 
over the place (I didn't modify anything in this server so this is its 
default mod_python configuration), but this works on the same file:

import mod_python

def index(req):
     req.content_type = "text/plain"
     return "Hello World!"

This is the kind of offending code I was talking about:

import cgi
import os

form = cgi.FieldStorage()
if form.has_key("dir"):
     root_dir = form["dir"].value
     if os.path.commonprefix([document_root, root_dir]) != document_root:
         print 'Invalid dir'
         sys.exit(1)
else:
     root_dir = document_root


Here the dir parameter comes from a get. Under plain old cgi 
form.has_key("dir") is true but not so under mod_python.

Another test:

import cgi
cgi.test()


If called as http://my.server/script.py?foo=bar I get an empty "Form 
Contents" section but the QUERY_STRING variable is foo=bar as expected.

Under CGI "Form Contents" is:

foo: <type 'instance'>
     MiniFieldStorage('foo', 'bar')

I found this to be weird. mod_python makes available both get and post 
variables from req.form and so does cgi.FieldStorage() unless the latter 
it's being run under mod_python in which case post variables are 
available but not get...

Of course one can rewrite the script to use req.form or 
util.FieldStorage() but an unmodified python cgi script won't work as 
expected... I always thought one of the goals of mod_python was to 
provide an upgrade path for cgi scripts but it seems many cgi scripts 
won't work out of the box with it.

Thanks,




More information about the Mod_python mailing list