[Tutor] POST MULTI PART DATA WITH PYTHON

Prasad, Ramit ramit.prasad at jpmorgan.com
Mon May 6 23:03:20 CEST 2013


Please post your comments after quoted text (either bottom posting or in-line) not before (top posting). I have fixed the order below.

> On Mon, May 6, 2013 at 10:35 PM, Ajin Abraham <ajin25 at gmail.com> wrote:
> > Hi,
> >    I was working on a python project that requires to upload an image
> > using httplib. Here i need to POST a MULTI PART DATA with Python using
> > inbuilt libraries. I know there are better libraries like requests and
> > poster. but i need to do it with httplib itself.
> >
> > I will attach a screenshot==> http://s1.postimg.org/qjjhy445q/file.jpg
> >
> > So here i think there is some problem with my code that the
> > 'Content-Length is different and i am getting a 404 not found
> > response. i placed the file "screenshot.png" together with my python
> > source file.
> >
> > Here is my source code==>http://bpaste.net/show/V2xbtcBSpYsNbwGPCmFM/
> >
> > Please tell me what's wrong in my code. and please don't suggest me
> > other libraries.
> >

Ajin Abraham wrote:
>     Actually i figured out the content length problem and
> modified the source a bit.
> http://bpaste.net/raw/lax7e7qMLY3bKM69su9k/
> but still there is 404 not found problem My requirement is this.
> "With httplib, i need to upload a file (png) with python at
> :http://xboz.5gbfree.com/screenshot/upload.php
> Regards,
> Ajin Abraham
> 
> Information Security Enthusiast.
> www.ajinabraham.com | www.defconkerala.org
> www.keralacyberforce.in | +91-9633325997

Your code is small enough, that you should just paste it directly
rather than putting it on a website. That will help others searching
this list too.

What is the 404 error text? You may need to create an error handler so 
Python will pass the response back rather than throwing an exception.

I have only used urllib2 (standard library) and I have not done
multi-part form data. The general technique below should work, although
I used it on an XML file rather than a picture.

class BetterHTTPErrorProcessor(urllib2.BaseHandler):
    def http_error_404(self, request, response, code, msg, hdrs):
        '''Do not throw exception but instead return response because response text will eventually
        have a useful message for WHY this was received.
        '''
        return response

def foo():
    opener = urllib2.build_opener( BetterHTTPErrorProcessor, urllib2.HTTPHandler( debuglevel=debug ) )
    request = urllib2.Request(url, data=xml, headers={'Content-Type':'text/xml; charset=utf-8', 'Content-Length':len(xml) } )
    response = opener.open( request )

You should notice that I did not turn the content length into a string. 
I have no idea if that matters or not.


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list