Setting up a Python CGI Sandbox on Windows

It makes sense sometimes to set up a sandbox for development of CGI.  By sandbox I mean a computer isolated from the web running all the programs needed to develop for CGI.  If you have a Windows machine there are fewer resources available online for setting up Apache, etc than for a Linux box.

Note that installing a Apache server does take some resources but I noticed no difference on the 3 GHz Pentium D machine with 1 Gb RAM that I was using. follow these steps in setting up a windows machine:
  • Install Python
    If you are going to put scripts on a server at an ISP such as 1and1 or GoDaddy you should be aware of the version of Python available to your hosting package.  For 1and1 I installed Python 2.5.  Also be aware that neither 1and1 nor GoDaddy use modpython at this time.  
  • Install Apache
    Use a standard install of Apache.  It should take only a few minutes to install and configure.  When you are setting up make it "localhost".
    Add python as a CGI by modifying the following:
    Options Indexes FollowSymLinks ExecCGI
    AddHandler cgi-script .cgi .py
  • Install Eclipse or equivalent IDE
    Eclipse is the best full featured IDE to use with Python.  Although there is no standard build with Python as the default language, you can install the Java version of Eclipse and then install PyDev easily. Other good Python IDEs include Boa Constructor, Wing, or Idle.
  • Install MySQL (optional)
    Building scripts that access a MySQL database in Python is trivial in complexity but extremely useful and powerful.  I would suggest installing MySQL even if you don't intend to use it immediately.  The resources used are minimal.
There are some tips to getting Python working in CGI.
1. Always browse the pages through Apache.
Note that viewing files in the filesystem through a browser works for most things on an html page but will not work for CGI.  For scripts to work they must be opened through the htdocs file system.  The address line of your browser should look like:
\\127.0.0.1\index.html or
\\localhost\index.html

If you open a file up through the file system the CGI will not work.
c:\Apache\htdos\index.html

2. Convert end of lines of scripts to Unix format:
Most editors have options to "show end of lines" and then a checkbox to convert from Unix to PC format.  you must have the end of lines set to Unix format.

3. State the path to the Python interpreter on the first line of the CGI script:
You must have one of the following lines as the first line of your Python CGI script:
   #!C:\Python25\Python.exe
   #!/usr/bin/python
The top line is used when you are debugging on a PC and the bottom is for a server such as 1and1.  I leave the lines as shown and then edit them once they are up on the server by deleting the first line.

4. Print a content type specifying HTML before printing any other output:
This can be done simply by adding the following line somewhere very early in your script:
print "Content-Type: text/html\n\n"
Note that the 2 end of lines are required.


5. Setup Python scripts to give debugging information:
Import the following to get detailed debugging information.
import cgitb; cgitb.enable()
An alternative if cgitb is not available is to do the following:
import sys
sys.stderr = sys.stdout

6. On the server the python script permissions must be set to execute.
After uploading your files be sure to edit the first line and set the permissions for the file to execute.

A short debugging program for your CGI 
(Remember most hosts DO NOT USE modpython)
This program will show what is entered on a form (both the keys and the values).

#!C:\Python25\Python.exe
#!/usr/bin/python

#4 things for python CGI
#Open file through the server not from filesystem
#file must be have linux end-of-line characters
#Must have the appropriate python line as first line of the file
#Print a "content-type" before any output

import cgi
import cgitb; cgitb.enable()
print "Content-Type: text/html\n\n"
form_data = cgi.FieldStorage()
print form_data.keys()
for name in form_data.keys():
   print "Key: " + name + " Value: " + str(form_data.getvalue(name)) + "<BR>"

This page used to have an error(s) in the code.  There is no ".values()" method in cgi.FieldStorage() and the value needed an str in the string concat.

Comments

Popular posts from this blog

Maintenance of a 30 Year Old CNC Milling Machine

DIY CNC Milling Machine Control Panel for EMC2

Power Supply for CNC