1#!/usr/bin/python 2# 3# Copyright Divye Kapoor 2008. 4# Distributed under the Boost Software License, Version 1.0. 5# (See accompanying file LICENSE_1_0.txt or copy at 6# http:#www.boost.org/LICENSE_1_0.txt) 7# 8# This program sets up a CGI application on localhost 9# It can be accessed by http://localhost:8000/cgi-bin/requestinfo.py 10# It returns the query parameters passed to the CGI Script as plain text. 11# 12import cgitb; cgitb.enable() # for debugging only 13import cgi 14import os, sys 15 16sys.stdout.write( "HTTP/1.0 200 Requestinfo.py Output follows\r\n" ) 17sys.stdout.write( "Content-type: text/plain; charset=us-ascii\r\n\r\n" ) 18sys.stdout.write( "\r\n" ) 19 20form = cgi.FieldStorage() 21qstring = "" 22qstring_dict = {} 23 24print "Headers: ",form.headers 25 26if os.environ.has_key("QUERY_STRING"): 27 qstring = os.environ["QUERY_STRING"] 28 try: 29 qstring_dict = cgi.parse_qs(qstring,1,1) # parse_qs(query_string,keep_blanks,strict_parsing) 30 except ValueError: 31 print "Error parsing query string." 32 33 34print "Query string:", qstring 35 36print "GET parameters:", 37for i in qstring_dict.keys(): 38 print i,"-",qstring_dict[i],";", 39print 40 41# Remove GET params and print only the POST ones 42print "POST parameters:", 43try: 44 for i in form.keys(): 45 if i not in qstring_dict.keys(): 46 print i,"-",form.getfirst(i, ""),";", 47 print 48except TypeError: # In case of empty POST bodies. 49 pass 50 51print 'HTTP HEADERS-------------------' 52print 'Content-Type:', os.environ.get('CONTENT_TYPE') 53print 'Content-Length:', os.environ.get('CONTENT_LENGTH') 54for k in os.environ.keys(): 55 if k.startswith('HTTP_'): 56 print k, ':', os.environ[k] 57