Monday, August 07, 2006

mod_python on Apache

Python .... hmmm, it's sound like a snake. But what i meant is a programming language. After succeed installing mod_python, i'm trying to add this line below in to the Apache httpd.conf :

LoadModule python_module libexec/mod_python.so
AddHandler python-program .py


Then i'm restarting the Apache web server. After that, i create a file named "test.py" under "pypublish" directory, and the code look like below :

def index():
return "This is only a test."
def peyton():
return "Peyton wrote this."


The problem is, why when i'm calling http://python.intranet/pypublish/test.py in the browser, the code above shown again. It's like that file not known as a python file or look like no handler for that file?

To solve the problem, in the httpd.conf configuration i'm adding the line like this below :

# To load python module:
LoadModule python_module libexec/mod_python.so

# Python Project
<VirtualHost 172.16.3.87:80
>
DocumentRoot /data/data/project/learn/python/
ServerName python.intranet
<Directory /data/data/project/learn/python/pypublish>
AddHandler python-program .py
PythonHandler mptest
PythonDebug On
</Directory>
</VirtualHost>

Sorry, i create a new file named "mptest.py" with the content :

from mod_python import apache

def handler(req):
req.send_http_header()
req.write("Hello Toserba Yogya IT&S Developer, welcome to the Python World!")
return apache.OK


And then when it running to the browser with http://python.intranet/pypublish/mptest.py the program running well.

No comments: