ActiveState Code

Recipe 52220: The Simplest CGI Program


The simplest CGI program. This script displays the current version of Python and the environment values.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/local/bin/python
print "Content-type: text/html"
print
print "<pre>"
import os, sys
from cgi import escape
print "<strong>Python %s</strong>" % sys.version
keys = os.environ.keys()
keys.sort()
for k in keys:
    print "%s\t%s" % (escape(k), escape(os.environ[k]))
print "</pre>"

Discussion

Every CGI programmer should have some simple code handy to drop into their cgi-bin directory. Run this script before wasting time slogging through your Apache configuration files. :-)

Comments

  1. 1. At 1:59 a.m. on 12 mar 2001, Carey Evans said:

    cgi.escape. Maybe it's a little picky, but you should import cgi and use cgi.escape() to quote any potential HTML in the response. i.e.

    print "%s\t%s" % (cgi.escape(k), cgi.ecape(os.environ[k]))
    

    Why? First, it makes the output more readable if you happen to include code for a "<" in your query. Second, it protects you from attacks like those described in the links below if someone should happen to find your script on a live web site.

    Apache information

    CERT Advisory CA-2000-02

    Zope Client Side Trojan information

    (I wonder if this site allows arbitrary scripts?) Carey Evans

  2. 2. At 6:43 a.m. on 12 mar 2001, Jeff Bauer (the author) said:

    RE: cgi.escape. I've updated the script per Carey's suggestion to include cgi.escape. Thanks. Jeff Bauer

  3. 3. At 1:16 p.m. on 17 mar 2001, Amos Latteier said:

    cgi.test() is even easier. Here's an even easier CGI program that I use for testing:

    !/usr/local/bin/python

    import cgi cgi.test()

    This handles printing headers, and displays environment and form variables. Amos Latteier

  4. 4. At 9:16 a.m. on 17 apr 2001, Dirk Holtwick said:

    Even smaller ... You may also wan't to make it shorter ;-)

    #!/usr/bin/python
    import cgi
    cgi.test()
    
  5. 5. At 7:53 a.m. on 27 jul 2001, Jeff Bauer (the author) said:

    cgi.test(). I may not have made my intention clear. This is called the "simplest" CGI program, not the "shortest". For a newbie Python programmer trying to write his first CGI script, the cgi.test() method hides too many details to be useful, IMO.

  6. 6. At 11:56 a.m. on 17 feb 2002, Jim Glenn said:

    New Programmer Likes. I just downloaded PYTHON, and this is the first script I found in the cookbook that showed how to make a quick web page.

    I appreciate it

    Thanks

  7. 7. At 5:44 a.m. on 24 jul 2002, Brendan Martin said:

    help please, I am completely new to python,

    I want to test if python is working on my hosting account. I am told it is!

    I have copied the text at the bottom of my post, i have then pasted it to notepad, saved it as ptest.cgi and secondly ptest.txt

    uploaded it to my cgi-bin, inside a folder called python, chomod 755 then go to my page: http://yourfeet.co.uk/cgi-bin/python/ptest.cgi

    or http://yourfeet.co.uk/cgi-bin/python/ptest.txt

    both give errors in log as : Premature end of script headers

    What am i missing out please . . .

    !/usr/local/bin/python

    print "Content-type: text/html" print print "

    "
    import os, sys
    from cgi import escape
    print "Python %s" % sys.version
    keys = os.environ.keys()
    keys.sort()
    for k in keys:
        print "%s\t%s" % (escape(k), escape(os.environ[k]))
    print "
    

    "

  8. 8. At 8:32 a.m. on 9 aug 2002, Hugh Cowan said:

    Perfect Example. Thanks for the excellant example. It does exactly what is says. Very simply shows how to create a CGI application. Gives an excellant starting point for which anyone can go from there.

    There is nothing worse than trying to get something to atleast work (produce a result). Your example does this -- quickly and easily. Once you have a result, you can then build on it knowing that fundamentally it will work.

    It was exactly what I was looking for.

    Hugh,

  9. 9. At 2:24 p.m. on 13 oct 2002, Noah Spurrier said:

    My own simple CGI (perhaps not the simplest :-).

    #!/usr/bin/env python
    # This is my minimal cgi template.
    # Another thing I do is wrap my entire script in a try block.
    # It's also good to wrap the import statements, because
    # sometimes these will raise exceptions too.
    try:
            import traceback, sys, os, cgi
            # The following makes errors go to HTTP client's browser
            # instead of the server logs.
            sys.stderr = sys.stdout
            cgi.test()
    except Exception, e:
            print 'Content-type: text/html\n'
            print
            print '&lt;html&gt;&lt;head&gt;&lt;title&gt;'
            print str(e)
            print '&lt;/title&gt;'
            print '&lt;/head&gt;&lt;body&gt;'
            print '&lt;h1&gt;TRACEBACK&lt;/h1&gt;'
            print '&lt;pre&gt;'
            traceback.print_exc()
            print '&lt;/pre&gt;'
            print '&lt;/body&gt;&lt;/html&gt;'
    
  10. 10. At 3:53 p.m. on 22 jun 2004, Dave Baker said:

    'Premature end of script headers' can be caused by using Windows as text editor. Brendan, your trouble with the 'Premature end of script headers' error message might be due to your having created the cgi script using a Windows text editor, and then uploading the script to your server using ftp.

    Windows text editors generally save text files with "\r\n" as the set of characters that marks the end of a line; you want to save the file as a "UNIX" format file, so that only "\n" marks the end of a line.

    Some more info is here:

    http://sundown.greyledge.net/pages/2003/03/notes_on_using_python_for_cgi_scripts_under_apache.html

  11. 11. At 11:12 a.m. on 23 nov 2004, merwyn soares said:

    Just vacation. Have a great vacation

Sign in to comment