Python Collecting Web Input

{ reset HTTP_POST_VARS; while list k, v = each HTTP_POST_VARS keys[k] = k; } return keys; } get_param_names ret urns a list of param et er nam es present in t he HTTP variable arrays, wit h duplicat e nam es rem oved if t here is overlap bet ween t he arrays. The ret urn value is an associat ive array wit h bot h t he keys and values set t o t he param et er nam es. This way you can use eit her t he keys or t he values as t he list of nam es. The following exam ple print s t he nam es, using t he values: param_names = get_param_names ; while list k, v = each param_names print htmlspecialchars v . br \n; For PHP 3 script s, t he param et ers in URLs should be separat ed by charact ers. That s also t he default for PHP 4, alt hough you can change it using t he arg_separator configurat ion set t ing in t he PHP init ializat ion file.

18.6.7 Python

The Pyt hon cgi m odule provides access t o t he input param et ers t hat are present in t he script environm ent . I m port t hat m odule, t hen creat e a FieldStorage obj ect using a m et hod of t he sam e nam e: import cgi param = cgi.FieldStorage The FieldStorage m et hod ret urns inform at ion for param et ers subm it t ed via eit her GET or POST request s, so you need not know which m et hod was used t o send t he request . The FieldStorage obj ect cont ains an elem ent for each param et er present in t he environm ent . You can get a list of available param et er nam es like t his: names = param.keys I f a given param et er, name , is single- valued, t he value associat ed wit h it will be a scalar t hat you can access as follows: val = param[name].value I f t he param et er is m ult iple- valued, param[name] w ill be a list of MiniFieldStorage obj ect s t hat have name and value at t ribut es. Each of t hese has t he sam e nam e it will be equal t o name and one of t he param et ers values. To creat e a list cont aining all t he values for such a param et er, do t his: val = [ ] for item in param[name]: val.append item.value You can dist inguish single- valued from m ult iple- valued param et ers by checking t he t ype. The following code shows how t o get t he param et er nam es and loop t hrough each param et er t o print it s nam e and value, print ing m ult iple- valued param et ers as a com m a-separat ed list : [3] [3] This code requires that you import the string and types module in addition to the cgi module. param = cgi.FieldStorage param_names = param.keys param_names.sort print pParameter names:, param_names, p items = [ ] for name in param_names: if type param[name] is not types.ListType: its a scalar ptype = scalar val = param[name].value else: its a list ptype = list val = [ ] for item in param[name]: iterate through MiniFieldStorage val.append item.value items to get item values val = string.join val, , convert to string for printing items.append type= + ptype + , name= + name + , value= + val print make_unordered_list items Pyt hon will raise an except ion if you t ry t o access a param et er t hat is not present in t he FieldStorage obj ect . To avoid t his, use has_key t o find out if t he param et er exist s: if param.has_key name: print parameter + name + exists else: print parameter + name + does not exist Single- valued param et ers have at t ribut es ot her t han value . For exam ple, a param et er represent ing an uploaded file has addit ional at t ribut es you can use t o get t he files cont ent s. This is discussed furt her in Recipe 18.9 . The cgi m odule expect s URL param et ers t o be separat ed by charact ers. I f you generat e a hyperlink t o a script based on t he cgi m odule and t he URL includes param et ers, dont separat e t hem wit h ; charact ers.

18.6.8 Java