Thursday, October 1, 2009

Configuring Apache to handle PSP (python server pages)

Note: You will need Python and mod_python installed in your server. If you have not done that, please follow the python install instructions

Configuring Apache HTTP Server to Handle PSP


After mod_python installation you must make Apache aware of it. For this purpose open APACHE_HOME/conf/httpd.conf for editing(for default Linux location of httpd.conf, consult your system documentation) and append the following lines at the end:
LoadModule python_module modules/mod_python.so
AddHandler mod_python .psp .psp_
PythonHandler mod_python.psp
PythonDebug On

The first line loads mod_python into Apache. The actual path of mod_python.so may vary but the installer should give you the proper path upon finishing. (On Linux most likely it would be libexec/mod_python.so.) The second line informs Apache which files should be handled by PythonHandler, specified on the third line (more about .psp_ usage in the section below). The last directive switches mod_python into debugging mode; when PythonDebug directive is set to On, it will dump traces for the first unhandled exception in the running application. Setting this flag is highly recommended for development purposes, but make sure you turn it off on production environments so as to not expose certain system internals to visitors.

If you would like mod_python pages to be used as default index pages, locate in httpd.conf the following line and change it from
DirectoryIndex index.html index.html.var

to
DirectoryIndex index.html index.psp index.html.var

Now restart Apache. On Windows you usually do that with the Apache Monitor in the system tray or by running net stop apache2 and then net start apache2; on Linux it's as simple as apachectl restart.

Using mod_python


PSP are plain text files that include Python code surrounded with <% and %> tags. Python expressions are enclosed within <%= and %> tags. Directives enable inclusion of other PSP pages; they are wrapped by <%@ and %> tags. The least frequently used tags are <%-- and --%> which, as you might guess, are simply comments that are never rendered to the resulting code.

Let's start with the simple Hello Python example. Under APACHE_HOME/htdocs create a file hello.psp with the following content:
<html>
<%
import sys
%>
Hello! You have <%=len(sys.modules)%> Python modules at your command.
</html>

Now go to http://127.0.0.1/hello.psp with your web browser. You will see how many Python modules are installed on your system. Of course, this information is trivial to us. Mod_python doesn't offer a function equivalent to PHP's phpinfo(), which gives detailed information about the interpreter. In the next step, we'll write one for PSP ourselves.

Under APACHE_HOME/htdocs (usually /var/www), create a file pspinfo.psp with the following content:
<%
req.content_type = 'text/plain'

import mod_python
import pprint
import sys
%>

PYTHON <%=sys.version %>
MOD_PYTHON <%=mod_python.version %>

PATH = <%=pprint.pformat(sys.path)%>

AVAILABLE MODULES
<%=pprint.pformat(sys.modules)%>

Now open http://127.0.0.1/pspinfo.psp and you should see a page as below:

_____________________________________________________________________
PYTHON 2.6.2 (release26-maint, Apr 19 2009, 02:11:59) 
[GCC 4.3.3]
MOD_PYTHON 3.3.1

PATH = ['/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/lib/python2.6/dist-packages',
'/usr/local/lib/python2.6/dist-packages']

AVAILABLE MODULES
{'StringIO': <module 'StringIO' from '/usr/lib/python2.6/StringIO.pyc'>,
'UserDict': <module 'UserDict' from '/usr/lib/python2.6/UserDict.pyc'>,
'__builtin__': <module '__builtin__' (built-in)>,
'__future__': <module '__future__' from '/usr/lib/python2.6/__future__.pyc'>,
'__main__': <module '__main__' (built-in)>,
'_abcoll': <module '_abcoll' from '/usr/lib/python2.6/_abcoll.pyc'>,
'_apache': <module '_apache' (built-in)>,
'_bisect': <module '_bisect' (built-in)>,
'_bsddb': <module '_bsddb' from '/usr/lib/python2.6/lib-dynload/_bsddb.so'>,
'_codecs': <module '_codecs' (built-in)>,
'_collections': <module '_collections' (built-in)>,
'_functools': <module '_functools' (built-in)>,
'_hashlib': <module '_hashlib' from '/usr/lib/python2.6/lib-dynload/_hashlib.so'>,
................
.............
..........
________________________________________________________________________________________

'_locale': <module '_locale' (built-in)>,

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.