Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse url form data

Status
Not open for further replies.

ephemeralz

Programmer
Dec 10, 2003
3
US
i need a function that decodes form data encoded in HTTP GET REQUEST URL:


and return a dictionary of key/value pairs representing the
form data. ‘+’ signs in values should be converted to spaces

{'user':'jdoe','name':['john doe']}

below is my attempt:
the problem to the code below is that it's not able to handle mutliple key with same values


import string

print context.standard_html_header(context,context.REQUEST)

firstKeyList=string.split(url,'?')
urlList=firstKeyList[-1]
urlKeyList=string.split(urlList,'&')
urlTuple = ('key',['values'])
urlList = range(len(urlKeyList))

urlTemp=''
urlFinal=''
urlDict= {}

for i in urlList:
urlValueList = string.split(urlKeyList,'=')
for j in range(len(urlValueList)-1):
string.join(string.split(urlValueList[j+1],'+')) + ")"
urlTuple = (urlValueList[j], "['" + string.join(string.split(urlValueList[j+1] + "']",'+')))
string.join(string.split(urlValueList[j+1],'+'))
string.join(string.split(urlValueList[j+1],'+')) + "'),"
urlList = urlTuple

urlList.reverse()
urlDict = dict(urlList)

print urlDict



print context.standard_html_footer(context,context.REQUEST)
return printed
 
You're reinventing the wheel, use the cgi module:

Code:
#!/usr/bin/env python
import cgi

form = cgi.FieldStorage()

urlDict = {}
for key in form.keys():
    urlDict[key] = form.getvalue( key )

print "Content-Type: text/plain\n\n"
print urlDict

yields:
{'three': ['1', 'plus', '2'], 'two': '1 plus 1', 'one': '1'}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top