I have to fill in multiple form fields on a web page
I got the http POST part completed so I can post data to the web page.
I also got the part completed where I create a dictionary of key/value pairs and have the form fields filled on the web page.
Key/value pairs:
input1 = {'hostname' : 'host', 'port' : '22', 'basedn' : 'CN=Users', 'bindusername' : 'admin', 'bindpassword' : 'passwd', 'groupname' : 'CN=Group,CN=Users,DC=tech,DC=com', 'usernameattribute' : 'name'}
for line in open("/Users/rwettstein/Scripts/Files/ldap-settings.txt", "r"):
print line
input = line
time.sleep(10)
params = urllib.urlencode(dict(input))
try:
f_handler = urlopen('https://hostname/path/file.php', params)
except urllib2.HTTPError, error:
print "Error Code: %s" % error.code
However, if I place the key/value pair information into a text file and then read the data from the text file, read from the file line by line, encode it into a dictionary, and then hand it off to the Http Request, I get the following error:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Is this error occurring because the value being passed from the file read function only returns one single argument?
Use ast.literal_eval to parse each line into a dict. I’ve modified your code a little to show how it’s done.