Development Island

Mod_python Setup Information

Setup
Our server is setup by default to use the Python Server Pages (PSP) and publisher handler in the following way:

AddHandler mod_python .py .psp .psp_
<FilesMatch "\.(psp|psp_)$">
	PythonHandler mod_python.psp
</FilesMatch>
<Files *.py>
	PythonHandler mod_python.publisher
</Files>
PythonDebug On
						

A ".htaccess" file can be used if a different configuration is needed.

Custom handlers
A ".htaccess" file can be used to specify your own python script that you wish to use a mod_python handler. The contents of the file similar to the following:

AddHandler mod_python .py
PythonHandler yourhandler
						

In the above example the "yourhandler" refers to your own handler Python script.

Execution
Please upload any Python pages into the "htdocs" directory of your website. It is best to use a file extension of ".py". Files do not need to be made executable.

Permissions
Your Python pages run as a different user than the FTP user you use to upload to your website. This means that your Python scripts can not modify your other web files unless you make any file or directories world writable.

For example if a Python file needs to modify an existing file on your site, that file must have its permissions set to "0646" or "0666". If a Python file needs to create a new file in an existing directory of your site you will need to make the directory world writable. The file mode for this would be either "0757" or "0777".

File modes can be set using your FTP client.

File format
Please upload all your programs in "UNIX" format. This can be done in windows using a text editor such as Ultra Edit. If you are using the Mac the standard text editor has a feature to save in this format.

Python is sensitive to whitespace, so you will need to ensure that your code is indented correctly.

Accessing your pages from the web
To access your page from a web browser or from a link in another page, you need to use a URL in the format of:

  • http://www.jbloggs.devisland.net/page.psp
  • http://www.jbloggs.devisland.net/page.py/handlername

Or just like this for a relative link:

  • /page.psp
  • /page.py/handlername

Debugging
Any errors that occur in your Python page will be sent either to the browser or to the server error log. We have enabled debugging support in the example ".htaccess" file above so any syntax errors will be traced and output to the browser.
You can view your error logs from our Members section by using the "View Logs" option or by downloading them via FTP from the "/logs" directory of your site.

Example PSP page
Here is an example page for use with the PSP handler:

<html>
	<body>
		<%
			import time
		%>
		Hello world, the time is: <%=time.strftime("%Y-%m-%d, %H:%M:%S")%>
	</body>
</html>
						

Example Publisher script
Here is an example script for use with the publisher hander:

from mod_python import apache
def index(req):
   req.content_type = "text/html"
   req.write("Hello world")
						

Disabling mod_python execution for a specific directory
To turn off mod_python all that is needed is to create a ".htaccess" file in the required directory with the following contents:

RemoveHandler mod_python .py
						

Dealing with the path
By default mod_python sets the path as "/", if you need to access any files you will need to set the correct path. The code below can be used to find the current path:

import os
currentPath = os.path.dirname(__file__)
						

If you need to access a directory outside of the "htdocs" then you can just change into the parent directory as shown below:

import os
newPath = os.path.dirname(__file__)
newPath += '/../'
						

Using local modules
If you need to use a different version of a module that we have installed, you can just upload it to a directory site and tell your scripts to look in there first. For example:

import sys
sys.path.append('mylibs')