0

I'm using MultipartPostHandler in file sending. My code is following:

params = {'file':open(file_name, 'rb')} 
headers = {'cookie':session_id}
urllib2.install_opener(urllib2.build_opener(MultipartPostHandler.MultipartPostHandler))
response = urllib2.urlopen(urllib2.Request("www.example.com/upload", params, headers))

How could I do the same (send file to the server) without using MultipartPostHandler? It would be good to use only buid-in python modules and urllib2. Is it possible.

2
  • What is the problem with MultipartPostHandler? It only uses built-ins, is freely licensed and the source code is available online. Commented Apr 2, 2012 at 8:31
  • MultipartPostHandler needs to install it using easy_install, pip or from source. I want like to write the python script that would not require new instalations. Commented Apr 2, 2012 at 9:20

2 Answers 2

1

MultipartPostHandler needs to install it using easy_install, pip or from source. I want like to write the python script that would not require new instalations.

Just add it to your original script - it is just one file. Copy paste the code for the module.

Sign up to request clarification or add additional context in comments.

Comments

0

Unfortunately, there is no direct method available to post a multiple part file using urllib2. But there are ways to accomplish that by writing a custom form object using mimetype and mimetools module. You could follow this recipe and adopt your form to do a mutipart upload using urllib2.

(In Python3, urllib.request data can take a pointer to file object and that does read the whole file into memory)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.