Standard Hello World from a Python CGI Script

well, mostly standard anyways...

This version is a self printing python program that demonstrates how to use python as a CGI script

This documents my experiences using DreamHost as my hosting provider. Your milage may vary

It is very important that Python CGI scripts...

  1. have '!#usr/bin/python' as the first line of the file
    this tells the system what interpreter to use / what language it is.
    This code, using that command, right now, on this host, is using Python 2.7.18 (default, Jul 1 2022, 12:27:04) [GCC 9.4.0]
    For specific versions of Python here at Dreamhost, you could use one of ...
    v2.1.3 -> #!/usr/bin/python2.1
    v2.2.1 -> #!/usr/bin/python2.2
    v2.3.5 -> #!/usr/bin/python2.3
    v2.4.6 -> #!/usr/bin/python2.4
    v2.5.2 -> #!/usr/bin/python2.5
    v2.7.3 -> #!/usr/bin/python2.7
    (noticing a pattern here?)
  2. end in '.py'
  3. print the 'Content-type: text/html\n' header in the first line if you want to view the output
  4. mark the file as executable: 'chmod 755 [filename.py]'

This is a self printing file, full source code follows
Or you could view the source directly helloworld.txt
#!/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 <b>Hello World</b> from a Python CGI Script<p />"
	print "well, <i>mostly standard</i> anyways...<p/>"
	print "This version is a self printing python program that demonstrates how to use python as a CGI script<p/> "
	print 'This documents my experiences using <a href="http://dreamhost.com">DreamHost</a> as my hosting provider.  <b>Your milage may vary</b><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 "this tells the system what interpreter to use / what language it is.<br/>"
	print "This code, using that command, right now, on this host, is using Python <code>%s</code><br/>" % sys.version
	print "For specific versions of Python here at Dreamhost, you could use one of ...<br/>"
	print "<strike>v2.1.3 -> <code>#!/usr/bin/python2.1</code></strike><br/>"
	print "<strike>v2.2.1 -> <code>#!/usr/bin/python2.2</code></strike><br/>"
	print "<strike>v2.3.5 -> <code>#!/usr/bin/python2.3</code></strike><br/>"
	print "<strike>v2.4.6 -> <code>#!/usr/bin/python2.4</code></strike><br/>"
	print "<strike>v2.5.2 -> <code>#!/usr/bin/python2.5</code></strike><br/>"
	print "v2.7.3 -> <code>#!/usr/bin/python2.7</code><br/>"
	print "(noticing a pattern here?)"
	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>"