The simplest CGI program. This script displays the current version of Python and the environment values.
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. :-)
Tags: cgi
cgi.escape. Maybe it's a little picky, but you should
import cgi
and usecgi.escape()
to quote any potential HTML in the response. i.e.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
RE: cgi.escape. I've updated the script per Carey's suggestion to include cgi.escape. Thanks. Jeff Bauer
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
Even smaller ... You may also wan't to make it shorter ;-)
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.
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
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 "
"
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,
My own simple CGI (perhaps not the simplest :-).
'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
Just vacation. Have a great vacation