def post_multipart(self, url, params, files):
"""
Generates and issues a multipart request for data files
:param url: a string, the url you are requesting
:param params: a dict, a key-value of all the parameters
:param files: a list, the list of tuples for your data
:returns: a dict parsed from the JSON response
"""
# combine the parameters with the generated oauth params
params = dict(params.items() + self.generate_oauth_params().items())
faux_req = oauth.Request(method="POST", url=url, parameters=params)
faux_req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), self.consumer, self.token)
params = dict(parse_qsl(faux_req.to_postdata()))
content_type, body = self.encode_multipart_formdata(params, files)
headers = {'Content-Type': content_type, 'Content-Length': str(len(body))}
# Do a bytearray of the body and everything seems ok
r = urllib2.Request(url, body, headers)
if self.proxy_url:
proxy = urllib2.ProxyHandler({'http': self.proxy_url, 'https': self.proxy_url})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
content = urllib2.urlopen(r).read()
return self.json_parse(content)
评论列表
文章目录