Search code examples
pythonmacoscgi

Python CGI not executing on Mac OSX 10.6.7


Recently I started reading Mark Lutz's "Programming Python - Fourth Edition". I am a mac user, using ActivePython and OSX 10.6.7. Anyways, everything was going fine until the first instance of CGI in the book. The code example creates a form, and uses a POST method for finding someone's name:

<html>
<title>Interactive Page</title>
<body>
<form method=POST action="cgi-bin/testing.cgi">
    <p><b>Enter your name:</b>
    <p><input type="text" name="user">
    <p><input type="submit">
</form>
</body>
</html>

The CGI script is then put into a subdirectory, as shown in the HTML and looks like this:

#!/usr/bin/python
import cgi
form = cgi.FieldStorage()
print("Content-type: text/html\n")
print("<title>Reply Page</title>"
if not 'user' in form:
    print("<h1>Who are you?</h1>")
else:
    print("<h1>Hello <i>%s</i>!</h1>" % cgi.escape(form['user'].value))

However when I load the html and run a test, the script does not execute. What occurs is that either the browser loads the entire script as Hypertext, or that I download a .py file; depending on the browser I use (Eclipse and Safari show me text, Chrome and Firefox download a file). If it means anything, when I open the .html file it opens using file:// and not http://.

I'm not sure if this could be a problem with my script, or with a variety of other things. If anyone has any suggestions on executing python-based CGI on a mac that would be great.


Solution

  • You need to configure and run a webserver, then access the file through that webserver with a url like http://localhost:8080/cgi/foo.py, and not the local path to the file.