Python urllib2 file upload problems
If you're using Python 2.5 or newer, urllib2_file
is both unnecessary and unsupported, so check which version you're using (and perhaps upgrade).
If you're using Python 2.3 or 2.4 (the only versions supported by urllib2_file
), try running the sample code and see if you have the same problem. If so, there is likely something wrong either with your Python or urllib2_file
installation.
EDIT:
Also, you don't seem to be using either of urllib2_file
's two supported formats for POST data. Try using one of the following two lines instead:
d = ['uploaded', open(sys.argv[1:])]
## --OR-- ##
d = {'uploaded': open(sys.argv[1:])}
Salty
I enjoy playing tennis, playing the piano, and hanging out with friends. I hate people who make noise in movie theaters. That's about it. =]
Updated on June 04, 2022Comments
-
Salty almost 2 years
I'm currently trying to initiate a file upload with urllib2 and the urllib2_file library. Here's my code:
import sys import urllib2_file import urllib2 URL='http://aquate.us/upload.php' d = [('uploaded', open(sys.argv[1:]))] req = urllib2.Request(URL, d) u = urllib2.urlopen(req) print u.read()
I've placed this .py file in my My Documents directory and placed a shortcut to it in my Send To folder (the shortcut URL is ).
When I right click a file, choose Send To, and select Aquate (my python), it opens a command prompt for a split second and then closes it. Nothing gets uploaded.
I knew there was probably an error going on so I typed the code into CL python, line by line. When I ran the
u=urllib2.urlopen(req)
line, I didn't get an error; alt text http://www.aquate.us/u/55245858877937182052.jpginstead, the cursor simply started blinking on a new line beneath that line. I waited a couple of minutes to see if something would happen but it just stayed like that. To get it to stop, I had to press ctrl+break.
What's up with this script?
Thanks in advance!
[Edit] Forgot to mention -- when I ran the script without the request data (the file) it ran like a charm. Is it a problem with urllib2_file?
[edit 2]:
import MultipartPostHandler, urllib2, cookielib,sys import win32clipboard as w cookies = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),MultipartPostHandler.MultipartPostHandler) params = {"uploaded" : open("c:/cfoot.js") } a=opener.open("http://www.aquate.us/upload.php", params) text = a.read() w.OpenClipboard() w.EmptyClipboard() w.SetClipboardText(text) w.CloseClipboard()
That code works like a charm if you run it through the command line.
-
archbishop about 14 yearsYou say that urllib2_file is unnecessary on python 2.5 and greater, but this functionality doesn't exist in python yet. Based on bugs.python.org/issue3244, multipart/form-data uploads may be here in 2.7, but that's not released yet.
-
Ben Blank about 14 yearsI haven't looked at the problem since I posted this more than a year ago, but at the time, I was able to duplicate urllib2_file's functionality in Python 2.6 and even the library's page says it is only needed for Python 2.3 and 2.4. I can only assume that bug refers to some separate (but probably closely related) issue. :-)