Skip to content

Instantly share code, notes, and snippets.

@isonno
Last active August 29, 2015 14:08
Show Gist options
  • Save isonno/e4114681cd88f4678812 to your computer and use it in GitHub Desktop.
Save isonno/e4114681cd88f4678812 to your computer and use it in GitHub Desktop.
Simple python demo script for koding.com. Unlike their sample, it shows how to gather information from the incoming URL.
#!/usr/bin/python
import os, platform, sys, cgi, cgitb
cgitb.enable() # Make debug info available
def printBreak(msg):
print "</pre>\n" + msg + "\n<pre>"
def printEnv(var):
print var[0]
print " " + var[1] + ": " + os.getenv(var[1])
print "Content-Type: text/html"
print
print """\
<!DOCTYPE html>
<html lang="en">
<H1>This is some server output</H1>
"""
if os.getenv("PATH_INFO") == "/server":
print "Just printing the server name: " + os.getenv("SERVER_NAME") + ":" + os.getenv("SERVER_PORT")
sys.exit(1)
print "<pre>"
printBreak( "CGI args:" )
args = cgi.FieldStorage()
for i in args.keys():
print i + ": " + args[i].value
# Note a typical URL looks like:
# http://xxyyzz.koding.io/test-server.py/something?Testing=honk2&SecondItem=gronk
printBreak( "Useful vars" )
for i in [("Everything after the domain","REQUEST_URI"),
("Just the path item after the script name","PATH_INFO"),
("Everything before the ?", "SCRIPT_URI"),
("Between the domain and the ?","SCRIPT_URL"),
("Everything after the ?", "QUERY_STRING")]:
printEnv(i)
printBreak( "Environ" )
cgi.print_environ()
print "\n</pre>"
print "Done."
print "</html>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment