Mod-python problem

Alan Kennedy alanmk at hotmail.com
Thu Feb 28 10:34:08 EST 2002


bp at sam.localdomain (Bob Parnes) wrote in message 
> Just in case I am missing something obvious, the following 
> is my test code, for a linux system:

Bob,

>From what I can see, your clTest class and cltest method are fine.
There are a few minor errors in the rest of the code though. I'll
repeat a complete version that works (for me anyway) at the bottom.

> #!/usr/bin/env python

While the shebang line is not a problem, it is not necessary for
mod_python. It is only necessary for CGI programs, so that the apache
daemon knows which interpreter to pass the script to.

> from mod_python import util

You're missing the following line here, see below for the reason why

from mod_python import apache

> 
> class clTest:
>     def cltest(self, req, formDict):

As I mentioned, these are fine, so I haven't repeated them.

> def main(req):

For mod_python, the default handler method is called "handler". You
can use other method names, but you have to explicitly mention this in
the httpd.conf, like so

#-- Extract from httpd.conf
<Directory /path/to/some/directory>
   AddHandler python-program .py
   PythonHandler formTest::main
</Directory>
#--

>     formDict = util.FieldStorage(req)
>     cl = clTest()

These two lines are fine.

>     return cl.cltest(req, formDict)

There are a couple of problems here.

1. The way to send output to the client is to use the "req.write"
method.
2. The return from the function should be a status value indicating
whether the request was successfully handled.

So this one line should be replaced with the two lines

    req.write(cl.cltest(req, formDict))
    return apache.OK

Note the "apache.OK" constant, which is why you need to import the
mod_python.apache module.

Also, you should really set a return content/MIME type for the output,
so the browser knows what to do with it. This is done like this

    req.content_type = "text/html"
    req.send_http_header()

So, the corrected version, which works for me, is as follows

# Script starts here

from mod_python import util
from mod_python import apache

class clTest:
    def cltest(self, req, formDict):
        res = '<html><head></head><body>'
        res += '<table align=center>'
        res += '<TR><TH colspan=2>Welcome to the \
            mod-python class test'
        res += '<TR><TD colspan=2>keys: %s' \
		    % formDict.keys()
        res += '<form method=post>'
        res += '<TR><TD>Name <TD><input type=text \
            name="name">'
        res += '<TR><TD>Phone <TD><input type=text \
            name="phone">'
        res += '<TR><TD colspan=2 align=center> \
            <input type=submit name=submit>'
        res += '</form></table></body></html>'
        return res

def handler(req):
    formDict = util.FieldStorage(req)
    cl = clTest()
    req.content_type = "text/html"
    req.send_http_header()
    req.write(cl.cltest(req, formDict))
    return apache.OK
    
# Script ends here

Hope that works.

Al.



More information about the Python-list mailing list