I want to fill some web-form with python script and send with POST-request. The problem is that form has fileuploading. I found this: poster for python. So, I came with FireBug at website with form and filled it. What I saw:
values[action] add_save
values[mod] blog
values[depth] 2
values[pid] 121
values[title] title
values[title_eng] title_en
img PNG...[a lot of binary image data]
That site uses authorization, so I have:
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = poster.streaminghttp.register_openers()
opener.add_handler(authhandler)
Now I came with default sample from website and wrote the code:
index = """some parsed integer"""
datagen, headers = multipart_encode({
'values[action]': 'add_save',
'values[mod]': 'blog',
'values[depth]': '2',
'values[pid]': index,
'values[title]' : 'title',
'values[title_eng]' : 'title_eng',
'img': open('1.png', 'rb')
})
request = urllib2.Request(theurl, datagen, headers)
getdata = urllib2.urlopen(request)
print getdata.read()
I don't have errors, etc. But after sending POST the script doesn't add it into database (when I do that by hands everything is fine).
I added this code:
for value in datagen:
print value
And the result is: link. (123 number in text is that index variable value).