urllib2 request with binary file as payload

Michiel Sikma michiel at wedemandhtml.com
Thu May 12 04:05:52 EDT 2011


>
> 2.6 is expecting a string, according to the above. No mention of file.
> Moreover it expects the data to be urlencoded. 2.7.1 docs say the same
> thing. Are you sure you have shown the code that worked with 2.6?
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Yes, in fact I simply tried passing the file to the Request class
without having checked the docs to see if I could even do that, but it
worked. I also send an unencoded XML string a little bit earlier on.
Maybe it no longer needs to be a urlencoded string as soon as you add
a Content-Type header.

Here's what happens when I put the HTTPHandler in debug mode and pass
a file in 2.6:

send: 'PUT /resumable/feeds/api/users/default/uploads?upload_id=[id]HTTP/1.1\r\nAccept-Encoding:
identity\r\nContent-Length: 898801\r\nHost:
uploads.gdata.youtube.com\r\nContent-Type:
application/octet-stream\r\nConnection: close\r\nUser-Agent:
Python-urllib/2.6\r\n\r\n'
send: <open file 'file.mp4', mode 'rb' at 0x1005db580>
sendIng a read()able

A little while later:

reply: 'HTTP/1.1 201 Created\r\n'
[...etc]

So it seems to work just fine, but 2.5 does indeed complain about this
being an incorrect use of Request.

Moreover, though I can't upgrade my Python from 2.5 I was able to
compile another alongside it (latest stable 2.7) and use that with my
code, and it worked just fine. Uploaded a 6GB file to Youtube this
way. :)

Michiel Sikma
We Demand HTML

http://wedemandhtml.com/
michiel at wedemandhtml.com



More information about the Python-list mailing list