Issue #7 open

Multipart form post doesn't work with HTTPDigestAuthHandler

cab938
created an issue

Here's an example:

{{{

!python

import urllib2 from poster.encode import multipart_encode from poster.streaminghttp import register_openers

register_openers()

authinfo = urllib2.HTTPDigestAuthHandler() authinfo.add_password( "realm","machine","user", "pass")

opener = urllib2.build_opener(authinfo) urllib2.install_opener(opener)

fields={"param":"someval","file":open("myfile.zip","rb")}

datagen, headers = multipart_encode(fields)

request = urllib2.Request('url', datagen, headers)

content='' item = urllib2.urlopen(request) for line in item.readlines(): content += line

}}}

It throws the following:

{{{

!fixedfont

{{{

!python

... /usr/lib/python2.6/httplib.pyc in request(self, method, url, body, headers) 908 909 try: --> 910 self._send_request(method, url, body, headers) 911 except socket.error, v: 912 # trap 'Broken pipe' if we're allowed to automatically reconnect

/usr/lib/python2.6/httplib.pyc in _send_request(self, method, url, body, headers) 948 949 if body: --> 950 self.send(body) 951 952 def getresponse(self):

/usr/lib/python2.6/httplib.pyc in send(self, str) 753 data=str.read(blocksize) 754 else: --> 755 self.sock.sendall(str) 756 except socket.error, v: 757 if v[0] == 32: # Broken pipe

/usr/lib/python2.6/socket.pyc in sendall(self, *args)

TypeError: sendall() argument 1 must be string or buffer, not instance

}}} }}}

Comments (1)

  1. Chris AtLee repo owner
    • changed status to open

    Hi, if you use the opener object returned by register_openers, I believe this will work for you. See the last example at http://atlee.ca/software/poster/#example

    import urllib2
    from poster.encode import multipart_encode
    from poster.streaminghttp import register_openers
    
    opener = register_openers()
    
    authinfo = urllib2.HTTPDigestAuthHandler()
    authinfo.add_password( "realm","machine","user", "pass")
    
    opener.add_handler(authinfo)
    
    fields={"param":"someval","file":open("myfile.zip","rb")}
    
    datagen, headers = multipart_encode(fields)
    
    request = urllib2.Request('url', datagen, headers)
    
    content=''
    item = urllib2.urlopen(request)
    for line in item.readlines():
            content += line
    
  2. Log in to comment