#!/usr/bin/python
'''this program can be used for testing the contents of
cgi.FieldStorage() for arbitrary pages'''
import cgi, os, cgitb
cgitb.enable()
# get and process input
storageContents = cgi.FieldStorage()
def getFieldStorage(aStorage):
'''returns a table with key/value pairs from a fieldstorage'''
atable = '
'
for k in aStorage.keys():
atable += '''
| %s |
%s |
''' %(k,aStorage[k].value)
atable += '\n
'
return atable
#define return page
def makeHeading(title='A title'):
'''returns the heading of the response page'''
return '''Content-type: text/html
%s
''' % (title)
def makeBody(dictionaryOfContents = {'key1':'1', 'key2':'2'}):
'''returns two tables. One shows the raw contents of the fieldstorage
The other shows key/value pairs'''
bodytext ='''
cgi-test
'''
for k in dictionaryOfContents.keys():
bodytext += '''
%s:
''' %(k,dictionaryOfContents[k])
bodytext += '''
'''
bodytext += ''
return bodytext
def makeEnding():
return '\n'
def makeReturnPage():
'''makes the whole page to be sent back'''
return makeHeading() \
+ makeBody({'raw cgi.FieldStorage':storageContents,'get cgi.FieldStorage':getFieldStorage(storageContents)}) \
+ makeEnding()
#send return page
print makeReturnPage()