pastebin - collaborative debugging tool
kpaste.net RSS


Python CGI
Posted by Admin on Tue 16th Mar 2010 00:07
raw | new post

  1. #!/usr/bin/python
  2.  
  3. ### Python CGI Example
  4. ###    Author: TorrentialStorm
  5.  
  6. import sys, os
  7. import cgi, urlparse, Cookie
  8.  
  9. import cgitb
  10. cgitb.enable()
  11.  
  12. def main():
  13.     print "Content-Type: text/html"
  14.     print
  15.  
  16.     QUERY_STRING = os.environ.get("QUERY_STRING")
  17.     if QUERY_STRING != None:
  18.          _GET = urlparse.parse_qs(QUERY_STRING, True)
  19.     else:
  20.          _GET = {}
  21.  
  22.  
  23.     HTTP_COOKIE = os.environ.get("HTTP_COOKIE")
  24.     _COOKIE = Cookie.SimpleCookie()
  25.     if HTTP_COOKIE != None:
  26.         _COOKIE.load(HTTP_COOKIE)
  27.  
  28.     print "Environment vars:<br/>"
  29.     for key in os.environ.keys():
  30.         print "%s<br/>" % (key)
  31.  
  32.     print "<br/><br/>GET vars:<br/>"
  33.     for key in _GET.keys():
  34.         print "GET[%s] = %s<br/>" % (key, _GET[key])
  35.  
  36.     print "<br/><br/>Cookies:<br/>"
  37.     for key in _COOKIE.keys():
  38.         print "COOKIE[%s] = %s<br/>" % (key, _COOKIE[key].value)
  39.  
  40.  
  41. if __name__ == '__main__':
  42.     main()

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at