views:

254

answers:

3

Hi,

Just installed and configured mod_python 3.2.8 on a CentOS 5 (Apache 2.2.3) server with Python 2.4.3. It is loaded fine by Apache.

I activated the mpinfo test page and it works. So I wrote a simple "Hello World" with the following code:

from mod_python import apache

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

It outputs a blank page, with no text and no source. If I consciously create a syntax error I get the error output on the URL, for example (when I put a space 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 have spent about five hours browsing different tutorials, faqs and trouble shooting guides but can't find a description of this exakt issue.

What do you think could be the issue/cause?

EDIT: Here is the Apache configuration for the site...

<Directory />
    Options FollowSymLinks
    AllowOverride None

    AddHandler mod_python .py
    PythonHandler mptest
    PythonDebug On 
</Directory>

EDIT 2: Ah, another thing I forgot to mention is that I intend to use mod_python to write Apache extensions. The application itself is written in PHP but I need to make some security tweeks on the server :)

+2  A: 

Don't use mod_python.

A common mistake is to take mod_python as "mod_php, but for python" and that is not true. mod_python is more suited to writing apache extensions, not web applications.

The standartized protocol to use between python web applications and web servers (not only apache) is WSGI. Using it ensures that you can publish your application to any wsgi-compliant webserver (almost all modern web servers are wsgi-compliant)

On apache, use mod_wsgi instead.

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

mywebapp.py:

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

Apache configuration:

WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/mywebapp.py
<Directory /usr/local/www/wsgi-scripts>
  Order allow,deny
  Allow from all
</Directory>

Now just go to http://localhost/myapp and the script will run. Additionally, any access under this root (i.e. http://localhost/myapp/stuff/here) will be handled by this script.

It's a good idea to choose a web framework. CherryPy. Pylons. Django. They make things even easier.

A good website to look at is wsgi.org

nosklo
Ah, another thing I forgot to mention is that I intend to use mod_python to write Apache extensions. The application itself is written in PHP but I need to make som security tweeks on the server :)However I'm soon going to experiment more with web development through Python and I will use Django then. Thank you for the information. It will be helpful for future apps :)
Christoffer
A: 

I make a complete new answer for clarity...

I decided to install mod_wsgi instead. So I've set it up and when I go to my testfile I just see the page source. I haven't been spending any time on finding the issue yet, so I'll get back to you when I either solve the problem or decide that I need more help :)

Thank you :)

Christoffer
Ok, so I did exactly (with tweeks to suit my server) what nosklo suggested with mod_wsgi 2.6, which I installed according to the guide on the project website. But the output of my testapp is the Python source code. I haven't found any tips through Google. What do you think?
Christoffer
@Christoffer: Don't put the `.py` file into your web root. The `.py` file should be in another folder, outside `/var/www` . I chose `/usr/local/www/wsgi-scripts` in the example. The `WSGIScriptAlias` apache config option will make the bridge between the path that must be requested from the server and the script that will be ran, so instead of accessing `http://yourserver/path/thescript.py` you just go `http://yourselver/pathdefinedinconfig/` . In the example on my answer it is `http://server/myapp` even when **that's not the name of the script** .
nosklo
Aha :) It works now! Thank you so much!
Christoffer
+1  A: 

Your original problem is that mod_python.cgihandler is being called to handle the request. This means your Python script file is being interpreted as a CGI script. Thus, no wonder it doesn't return anything.

You likely have conflicting definition in your Apache configuration which is enabling the mod_python.cgihandler.

Graham Dumpleton