1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
#!/usr/local/bin/python
#
# A class for wrapping the WWW Forms Common Gateway Interface (CGI)
# Michael McLay, NIST mclay@eeel.nist.gov 6/14/94
#
# modified by Steve Majewski <sdm7g@Virginia.EDU> 12/5/94
#
# Several classes to parse the name/value pairs that are passed to
# a server's CGI by GET, POST or PUT methods by a WWW FORM. This
# module is based on Mike McLay's original cgi.py after discussing
# changes with him and others on the comp.lang.python newsgroup, and
# at the NIST Python workshop.
#
# The rationale for changes was:
# The original FormContent class was almost, but not quite like
# a dictionary object. Besides adding some extra access methods,
# it had a values() method with different arguments and semantics
# from the standard values() method of a mapping object. Also,
# it provided several different access methods that may be necessary
# or useful, but made it a little more confusing to figure out how
# to use. Also, we wanted to make the most typical cases the simplest
# and most convenient access methods. ( Most form fields just return
# a single value, and in practice, a lot of code was just assuming
# a single value and ignoring all others. On the other hand, the
# protocol allows multiple values to be returned.
#
# The new base class (FormContentDict) is just like a dictionary.
# In fact, if you just want a dictionary, all of the stuff that was
# in __init__ has been extracted into a cgi.parse() function that will
# return the "raw" dictionary, but having a class allows you to customize
# it further.
# Mike McLay's original FormContent class is reimplemented as a
# subclass of FormContentDict.
# There are two additional sub-classes, but I'm not yet too sure
# whether they are what I want.
#
import string,regsub,sys,os,urllib
# since os.environ may often be used in cgi code, we name it in this module.
from os import environ
def parse():
if environ['REQUEST_METHOD'] == 'POST':
qs = sys.stdin.read(string.atoi(environ['CONTENT_LENGTH']))
environ['QUERY_STRING'] = qs
else:
qs = environ['QUERY_STRING']
name_value_pairs = string.splitfields(qs, '&')
dict = {}
for name_value in name_value_pairs:
nv = string.splitfields(name_value, '=')
if len(nv) != 2:
continue
name = nv[0]
value = urllib.unquote(regsub.gsub('+',' ',nv[1]))
if len(value):
if dict.has_key (name):
dict[name].append(value)
else:
dict[name] = [value]
return dict
# The FormContent constructor creates a dictionary from the name/value pairs
# passed through the CGI interface.
#
# form['key']
# form.__getitem__('key')
# form.has_key('key')
# form.keys()
# form.values()
# form.items()
# form.dict
class FormContentDict:
def __init__( self ):
self.dict = parse()
self.query_string = environ['QUERY_STRING']
def __getitem__(self,key):
return self.dict[key]
def keys(self):
return self.dict.keys()
def has_key(self, key):
return self.dict.has_key(key)
def values(self):
return self.dict.values()
def items(self):
return self.dict.items()
def __len__( self ):
return len(self.dict)
# This is the "strict" single-value expecting version.
# IF you only expect a single value for each field, then form[key]
# will return that single value ( the [0]-th ), and raise an
# IndexError if that expectation is not true.
# IF you expect a field to have possible multiple values, than you
# can use form.getlist( key ) to get all of the values.
# values() and items() are a compromise: they return single strings
# where there is a single value, and lists of strings otherwise.
class SvFormContentDict(FormContentDict):
def __getitem__( self, key ):
if len( self.dict[key] ) > 1 :
raise IndexError, 'expecting a single value'
return self.dict[key][0]
def getlist( self, key ):
return self.dict[key]
def values( self ):
lis = []
for each in self.dict.values() :
if len( each ) == 1 :
lis.append( each[0] )
else: lis.append( each )
return lis
def items( self ):
lis = []
for key,value in self.dict.items():
if len(value) == 1 :
lis.append( (key,value[0]) )
else: lis.append( (key,value) )
return lis
# And this sub-class is similar to the above, but it will attempt to
# interpret numerical values. This is here as mostly as an example,
# but I think the real way to handle typed-data from a form may be
# to make an additional table driver parsing stage that has a table
# of allowed input patterns and the output conversion types - it
# would signal type-errors on parse, not on access.
class InterpFormContentDict(SvFormContentDict):
def __getitem__( self, key ):
v = SvFormContentDict.__getitem__( self, key )
if v[0] in string.digits+'+-.' :
try: return string.atoi( v )
except ValueError:
try: return string.atof( v )
except ValueError: pass
return string.strip(v)
def values( self ):
lis = []
for key in self.keys():
try:
lis.append( self[key] )
except IndexError:
lis.append( self.dict[key] )
return lis
def items( self ):
lis = []
for key in self.keys():
try:
lis.append( (key, self[key]) )
except IndexError:
lis.append( (key, self.dict[key]) )
return lis
# class FormContent parses the name/value pairs that are passed to a
# server's CGI by GET, POST, or PUT methods by a WWW FORM. several
# specialized FormContent dictionary access methods have been added
# for convenience.
# function return value
#
# form.keys() all keys in dictionary
# form.has_key('key') test keys existance
# form[key] returns list associated with key
# form.values('key') key's list (same as form.[key])
# form.indexed_value('key' index) nth element in key's value list
# form.value(key) key's unstripped value
# form.length(key) number of elements in key's list
# form.stripped(key) key's value with whitespace stripped
# form.pars() full dictionary
class FormContent(FormContentDict):
# This is the original FormContent semantics of values,
# not the dictionary like semantics.
def values(self,key):
if self.dict.has_key(key):return self.dict[key]
else: return None
def indexed_value(self,key, location):
if self.dict.has_key(key):
if len (self.dict[key]) > location:
return self.dict[key][location]
else: return None
else: return None
def value(self,key):
if self.dict.has_key(key):return self.dict[key][0]
else: return None
def length(self,key):
return len (self.dict[key])
def stripped(self,key):
if self.dict.has_key(key):return string.strip(self.dict[key][0])
else: return None
def pars(self):
return self.dict
def print_environ_usage():
print """
<H3>These operating system environment variables could have been
set:</H3> <UL>
<LI>AUTH_TYPE
<LI>CONTENT_LENGTH
<LI>CONTENT_TYPE
<LI>DATE_GMT
<LI>DATE_LOCAL
<LI>DOCUMENT_NAME
<LI>DOCUMENT_ROOT
<LI>DOCUMENT_URI
<LI>GATEWAY_INTERFACE
<LI>LAST_MODIFIED
<LI>PATH
<LI>PATH_INFO
<LI>PATH_TRANSLATED
<LI>QUERY_STRING
<LI>REMOTE_ADDR
<LI>REMOTE_HOST
<LI>REMOTE_IDENT
<LI>REMOTE_USER
<LI>REQUEST_METHOD
<LI>SCRIPT_NAME
<LI>SERVER_NAME
<LI>SERVER_PORT
<LI>SERVER_PROTOCOL
<LI>SERVER_ROOT
<LI>SERVER_SOFTWARE
</UL>
"""
def print_environ():
skeys = environ.keys()
skeys.sort()
print '<h3> The following environment variables ' \
'were set by the CGI script: </h3>'
print '<dl>'
for key in skeys:
print '<dt>', escape(key), '<dd>', escape(environ[key])
print '</dl>'
def print_form( form ):
skeys = form.keys()
skeys.sort()
print '<h3> The following name/value pairs ' \
'were entered in the form: </h3>'
print '<dl>'
for key in skeys:
print '<dt>', escape(key), ':',
print '<i>', escape(`type(form[key])`), '</i>',
print '<dd>', escape(`form[key]`)
print '</dl>'
def escape( s ):
s = regsub.gsub('&', '&', s) # Must be done first
s = regsub.gsub('<', '<', s)
s = regsub.gsub('>', '>', s)
return s
def test( what ):
label = escape(str(what))
print 'Content-type: text/html\n\n'
print '<HEADER>\n<TITLE>' + label + '</TITLE>\n</HEADER>\n'
print '<BODY>\n'
print "<H1>" + label +"</H1>\n"
form = what()
print_form( form )
print_environ()
print_environ_usage()
print '</body>'
if __name__ == '__main__' :
test_classes = ( FormContent, FormContentDict, SvFormContentDict, InterpFormContentDict )
test( test_classes[0] ) # by default, test compatibility with
# old version, change index to test others.
|