Python upload file using urllib2




















Active Oldest Votes. Improve this answer. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New Post. Michael Foord. Does anyone know how to do a 'file upload' using urllib2? There is an example of doing file upload using httplib - on activestate python cookbook.

I have code that uses a request object to make requests using urllib2. It would also mean duplicating the code Has anyone done this or know the right incantations to do it?

Follow Post Reply. John J. John Interesting, I'll give it a try. This discussion thread is closed Start new discussion. Similar topics Python. The last line of the output shows our custom value.

You can set the outgoing data on the Request to post it to the server. Each call replaces the previous data. Encoding files for upload requires a little more work than simple forms. A complete MIME message needs to be constructed in the body of the request, so that the server can distinguish incoming form fields from uploaded files. If you need to add support for other URL types, you can register your own protocol handler to be invoked as needed.

The protocol open method takes a single argument, the Request instance, and it should return an object with a read method that can be used to read the data, an info method to return the response headers, and geturl to return the actual URL of the file being read. A simple way to achieve that is to create an instance of urllib. Since this is just a simulation, FauxNFSHandler is primed with the name of a temporary directory where it should look for all of its files.



0コメント

  • 1000 / 1000