Welcome, guest | Sign In | My Account | Store | Cart

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

Python, 12 lines
 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>"

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. :-)

11 comments

Carey Evans 20 years, 9 months ago  # | flag

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

Jeff Bauer (author) 20 years, 9 months ago  # | flag

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

Amos Latteier 20 years, 8 months ago  # | flag

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

Dirk Holtwick 20 years, 7 months ago  # | flag

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

#!/usr/bin/python
import cgi
cgi.test()
Jeff Bauer (author) 20 years, 4 months ago  # | flag

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.

Jim Glenn 19 years, 9 months ago  # | flag

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

Brendan Martin 19 years, 4 months ago  # | flag

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 "

"

Hugh Cowan 19 years, 4 months ago  # | flag

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,

Noah Spurrier 19 years, 1 month ago  # | flag

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;'
Dave Baker 17 years, 5 months ago  # | flag

'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

merwyn soares 17 years ago  # | flag

Just vacation. Have a great vacation