#!/usr/bin/python
# for linux
import sys
def main():
print “Content-type: text/html\n”
print “<HTML><TITLE>Hello World from Python</TITLE><body>”
print “Standard Hello World from a Python CGI Script<p />”
print “It is very important that Python CGI scripts…”
print “<ol>”
print “<li>have ‘<code>!#usr/bin/python</code>’ ”
print “as the first line of the file<br/>”
print “By default, this will get you Python %s<br/>” % sys.version
print “For specific versions of Python here at Dreamhost…<br/>”
print “v2.1.3 -> <code>#!/usr/bin/python2.1</code><br/>”
print “v2.2.1 -> <code>#!/usr/bin/python2.2</code><br/>”
print “v2.3.5 -> <code>#!/usr/bin/python2.3</code><br/>”
print “</li>”
print “<li>end in ‘<code>.py</code>’</li>”
print “<li>print the ‘<code>Content-type: text/html\\n</code>’ header”
print ” in the first line if you want to view the output</li>”
print “<li>mark the file as executable: ”
print “‘<code>chmod 755 [filename.py]</code>’</li>”
print “</ol>”
print “<hr />”
print “This is a self printing file, full source code follows<br/>”
print ‘Or you could view the source directly ‘
print ‘<a href=”helloworld.txt”>helloworld.txt</a>’
print “<hr />”
f = open(’helloworld.py’)
buff = f.readlines()
print “<pre>”
for line in buff:
# remove trailing newline
line = line.rstrip()
# escape less than/greater thans from < > to &lt; &gt; so they are viewable in html
# side effect: in a web browser it will look like it’s doing nothing ![]()
line = line.replace(”<”,”<”)
line = line.replace(”>”,”>”)
print line
print “</pre>”
print “</body></HTML>”
if( __name__ == “__main__”):
try:
main()
except:
print “Content-type: text/html\n”
print “<HTML>”
print “<title>error?</title>”
print “<body>”
print “There was an error on the page<p/>”
print “Notes:<br/>”
print “ exception handling is your friend</br>”
print “ cgitb only works with current versions (>=2.3 ?) of Python<br/>”
print “ not even an except block can recover from a print ”
print “without a closing quote - you’ll get an error 500 from the server”
print “<body>”
print “</HTML>”
#EndOfFile