#!/usr/bin/python # for debugging purposes import cgitb; cgitb.enable() # -- header ---------------------------------------------------------- print "Content-type: text/html" print # -- body ------------------------------------------------------------ import cgi, os, string table = cgi.parse_qs(os.environ["QUERY_STRING"]) key_list_list = []; even_row = 0; for key in table: key_string = cgi.escape(key) for val in table[key]: if even_row: color = "lightgrey" even_row = 0 else: color = "white" even_row = 1 key_list_list.append(''' %s %s ''' % (color, key_string, cgi.escape(val))) key_string = "" pair_list = [] ; even_row = 0 for (key,val) in cgi.parse_qsl(os.environ["QUERY_STRING"]): if even_row: color = "lightgrey" even_row = 0 else: color = "white" even_row = 1 pair_list.append(''' %s %s ''' % (color, cgi.escape(key), cgi.escape(val))) key_list_table = ''' %s
KeyValue
''' % string.join(key_list_list, '\n') pair_table = ''' %s
KeyValue
''' % string.join(pair_list, '\n') body = """

Key, val-list table

%s

Pair/Val table

%s """ % (key_list_table, pair_table) # insert body in template f = open('template.html') doc = f.read() f.close() print doc.replace('', 'Query String Example') \ .replace('', body)