views:

1009

answers:

3

I'm trying to get mod-python to work with apache2 but not having any success. I've followed a few tutorials for getting mod-python working but I can't see what I'm doing wrong.

When I visit http://site.example.com/cgi-bin/test.py I actually get my 404 page! (whereas I get a 403 forbidden if the file really doesn't exist)

Here's my setup:

In /etc/apache2/sites-enabled/ there are configuration files named after each of my domains. In the site.example.com file I've added this to the user directives section:

# Begin user directives <--
<Directory /home/default/site.example.com/user/htdocs/cgi-bin/>
     Options Indexes FollowSymLinks MultiViews
     AllowOverride AuthConfig
     Order allow,deny
     allow from all
     AddHandler mod_python .py
     PythonHandler mod_python.publisher
     PythonDebug On
</Directory>
# --> End user directives

full file here, which includes the line:

EDIT to add contents of test.py:

#!/usr/bin/python  
print "Content-type: text/html"  
print  
print 'hello world'  
#print 1/0

As shown above, I get a 404 /python/test.py was not found on this server.

But if I uncomment the last line I get:

Mod_python error: "PythonHandler mod_python.publisher"

Traceback (most recent call last):

  File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
    result = object(req)

  File "/usr/lib/python2.4/site-packages/mod_python/publisher.py", line 98, in handler
    path=[path])

  File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 457, in import_module
    module = imp.load_module(mname, f, p, d)

  File "/home/default/site.example.co.uk/user/htdocs/python/test.py", line 5, in ?
    print 1/0

ZeroDivisionError: integer division or modulo by zero

Does this traceback look right? Enabling cgitb has no effect by the way.

Is there any other info I can add to help diagnose this?

+1  A: 

For starters, I would suggest not using the cgi-bin directory because that's intended for CGI scripts. Python files that get loaded with mod_python are not CGI scripts. (Theoretically you should be able to load Python files from the cgi-bin directory, but it'll be easier to debug if you give your Python files their own directory.)

Now, your document root is /home/default/site.example.com/user/htdocs so I'd suggest creating a subdirectory of that, say /home/default/site.example.com/user/htdocs/python, to put your Python files in - just for testing. Don't create any Alias or ScriptAlias directives for this directory (because it's under the document root already), but you'll need to put something like this in your virtual host file:

<Directory /home/default/site.example.com/user/htdocs/python/>
     Order allow,deny
     Allow from all
     AddHandler mod_python .py
     PythonHandler mod_python.publisher
     PythonDebug On
</Directory>

.Move the file test.py to that directory and try going to http://site.example.com/python/test.py, and you should see the results of the index function in the file test.py.

David Zaslavsky
Thanks, just tried that, I get this wierd error mode: if the script has as error it's shown; script has no error... raises 404!
Tom Viner
How about posting the contents of test.py? (edit it into your question)
David Zaslavsky
did you restart apache after editing the config file ?
Martin
+2  A: 

As far as I know, the Publisher handler needs to take in a request object and a method in order to return the response.

In my scripts, I have something like this:

#!/usr/bin/python  
def run(req):
  return 'Hello world!'

Then, go to http://.../python/test.py/run

In this case, req is the request object that gets passed in by the Publisher Handler.

And, in case you don't have one already, you need to have an __init__.py file in the same directory where test.py is.

Also, with the Publisher, I noticed that sometimes, the response gets cached. I will get the same errors back in the browser that I had already fixed. So, it's not a bad idea to restart apache if you see an error that you think you fixed already.

landyman
Ok brill that works now! Thanks.With the server based python I've used in the past you just print your output to the user. What are the different options for PythonHandler? Which of them was I using when I printed straight to output (including headers)? Don't think it was PSP. Python as CGI script?
Tom Viner
Well, it could either have been Python as a CGI script, or the CGI Handler in mod_python. They're pretty much the same. If you want to use mod_python in that way, use this in your Apache conf file instead: PythonHandler mod_python.cgihandlerThen, your normal python CGI scripts should work.
landyman
+1 . I had PythonHandler mod_python.publisher. Had to change it to PythonHandler mod_python.cgihandler to make python scripts work as cgi.
AJ
A: 

The python file should look like this in order to get the desired output:

def index():
  return "hello world"

"http://somesite.com/path/test.py" opens the function "index" by default.

"http://somesite.com/path/test.py/run" opens the function "run" instead.

You can get fancier, but the above will give you a basic mod_python page.

Thomas Weigel