Just installed and set up mod_python 3.2.8 on the CentOS 5 (Apache 2.2.3) server with Python 2.4.3. It's loaded fine by Apache.

I triggered the mpinfo test page and delay pills work. And So I authored an easy "Hello World" using the following code:

from mod_python import apache

def handler(req):
    req.content_type = 'text/plain'
    req.write("Hello World!")
    return apache.OK

It results an empty page, without any text with no source. Basically purposely produce a syntax error I recieve the mistake output around the URL, for instance (after i put an area before "def"):

Mod_python error: "PythonHandler mod_python.cgihandler"

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/cgihandler.py", line 96, in handler
    imp.load_module(module_name, fd, path, desc)

  File "/var/www/vhosts/localhost/httpdocs/mptest.py", line 3

    def handler(req):


SyntaxError: invalid syntax

I've spent about five hrs browsing different lessons, frequently asked questions and problem solving guides but aren't able to find an account of the exakt problem.

What is your opinion may be the problem/cause?

EDIT: This is actually the Apache configuration for that site...

<Directory />
    Options FollowSymLinks
    AllowOverride None

    AddHandler mod_python .py
    PythonHandler mptest
    PythonDebug On 

EDIT 2: Ah, another factor I didn't remember to say is the fact that I plan to use mod_python to create Apache extensions. The applying is designed in PHP but I have to have security tweeks around the server :)

Avoid using mod_python.

A typical mistake would be to take mod_python as "mod_php, however for python" which is not the case. mod_python is much more suitable for writing apache extensions, not web programs.

The standartized protocol to make use of between python web programs and web servers (not just apache) is WSGI. Utilizing it guarantees that you could publish the application to the wsgi-compliant webserver (just about all modern web servers are wsgi-compliant)

On apache, use [cde] rather.

Your example rewritten while using wsgi standard and mod_wsgi on apache:



Apache configuration:

def application(environ, start_response):
    start_response('200 OK', [('content-type', 'text/plain')])
    return ['Hello World']

Now go to WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/mywebapp.py <Directory /usr/local/www/wsgi-scripts> Order allow,deny Allow from all </Directory> and also the script will run. Furthermore, any access under this root (i.e. http://localhost/myapp) is going to be handled with this script.

It's wise to select an internet framework. CherryPy. Pylons. Django. They create things even simpler.

A good web site to check out is wsgi.org

Your original issue is that mod_python.cgihandler has been known as to handle request. What this means is your Python script file has been construed like a CGI script. Thus, no surprise it does not return anything.

You have in all probability conflicting definition inside your Apache configuration that is enabling the mod_python.cgihandler.

I create a complete new answer for clearness...

I made the decision to set up mod_wsgi rather. So I have arrange it so when Time passes to my testfile I simply begin to see the page source. I've not been investing whenever on locating the problem yet, so I'll respond after i either solve the issue or choose that I want more help :)

Thanks :)