Creating CGI scripts with MacPython

Note: this document is work-in-progress, and not tested.

MacPython allows you to use Python as a CGI scripting language for your webserver. Moreover, there are some helper applications that allow you to write your CGI scripts in the same way as for Unix and Windows, i.e. obtaining their arguments from os.environ and sending their results to stdout. MacPython also has all the facilities to program CGI scripts in the Macintosh way, through AppleEvents, but that falls outside the scope of this document (reading the source code for the helper scripts will give you an idea about how to do this).

For the purpose of this document we will assume you are using Apple's Personal Webserver 1.5, which comes standard with your machine as of MacOS 9.

The first step is to start the webserver, you will find it in your control panels. Give it a folder to serve documents from, and check that it is working by pointing your browser at it.

The next step is to tell the webserver that documents with a ".py" extension should be served through the PythonCGISlave helper application. Open the webserver, choose Preferences, tab Action, Add, Start on Extension, extension ".py", select application PythonCGISlave (which lives in Python:Mac:Tools:CGI).

The last step is to try it. Put the following script in Macintosh HD:Webpages:hello.py (assuming your webserver has the default settings for its document folder):

print "Content-type: text/plain"
print
print "Hello world!"
import time
print "Local time is", time.ctime(time.time())

Point your webbrowser at http://localhost/hello.py and check whether it works.