I am a newbie to web development, so please be mercy if the following question sounds a bit naive. How can I post to a web form using python? I know the web form is something like this, and I want to upload a local file for it to be processed online and get the returned data
<form action="process.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" id="chooser" /><br />
<input type="submit" value="Upload" name="submit" />
</form>
Here's what I have for my python program so far.
upfile = urllib.urlencode( {'file':path/to/my/local/file})
filehandle = urllib.urlopen('http://www.comdroid.org/', upfile)
soup = BeautifulSoup(filehandle)
print soup
I want to see the returned results, but the current code snippet only gives me the result without processing my uploaded file. Is there anything I am missing
------------UPDATE---------------- Here is how I do it now
import urllib2
import urllib
import requests
import os
files1 ={'file':open('/opt/apps/au.com.psyborg.sbc-5.apk','rb')}
#response= os.system("curl --form file=@/opt/apps/au.com.psyborg.sbc-5.apk --form submit=Upload www.comdroid.org/process.php")
req = requests.post('comdroid.org/process.php', files = files1)
print req.text
The weird thing is I cannot do it with request, the server complains my file is not .apk file, which it actually is. But if I switch to curl using os.system as commented out, it works! So I doubt I am missing something here in request. Any suggestion?