Posts Tagged ‘python’
My mod_python 101
After having built mod_python.so and doing a LoadModule modules/mod_python.so I expected everything to work fine, assuming I did a SetHandler python-program and a PythonHandler helloworld within an Apache virtualhost.
I could get Hello World! on the page then, with this following snippet:
$cat helloworld.py
from mod_python import apache
def handler(req):
req.content_type = “text/plain”
req.write(”Hello World!”)
return apache.OK
That’s when the problems began.
1. I couldn’t put in another .py file in the same directory that could run successfully – 404 returned
2. All HTML files returned a 404 as well
3. There was nothing on the error logs
After much reading around, here’s the final configuration that worked:
--snip--
LoadModule modules/mod_python.so
--snip--
PythonDebug On
AddHandler python-program .py
PythonHandler mod_python.publisher
PythonPath "['/appl/python2.5/lib/','/appl/webdocs/','/appl/python2.5/bin/python/','/appl/python2.5/lib/python2.5/site-packages/mod_python/','/appl/python2.5/lib/python2.5/site-packages/'] + sys.path"
--snip--
And the URL that worked was http://myhost/helloworld.py/handler
Apparently, mod_python (also) has three kinds of default handlers – cgi, publisher and PSP.
cgihandler – to work with existing CGI scripts by creating a false environment
psp – Python Server Pages, just like JSP
publisher – The preferred way to handle python pages, using page/method
publisher is recommended for newer applications, and as you move forward and your application increases in complexity and entry points, you can make your own handlers.
Like I said, 101 and basics, but what the heck, I get to write something here. Itchy fingers.
More later.
