def _prepare_http_request(self, uri, params, method='POST'):
# install error processor to handle HTTP 201 response correctly
if self.opener is None:
self.opener = urllib2.build_opener(HTTPErrorProcessor)
urllib2.install_opener(self.opener)
proxy_url = self.proxy_url
if proxy_url:
proxy = proxy_url.split('http://')[1]
proxyhandler = urllib2.ProxyHandler({'http': proxy})
opener = urllib2.build_opener(proxyhandler)
urllib2.install_opener(opener)
if method and method == 'GET':
uri = self._build_get_uri(uri, params)
_request = HTTPUrlRequest(uri)
else:
_request = HTTPUrlRequest(uri, urllib.urlencode(params))
if method and (method == 'DELETE' or method == 'PUT'):
_request.http_method = method
_request.add_header('User-Agent', self.USER_AGENT)
if self.auth_id and self.auth_token:
# append the POST variables sorted by key to the uri
# and transform None to '' and unicode to string
s = uri
for k, v in sorted(params.items()):
if k:
if v is None:
x = ''
else:
x = str(v)
s += k + x
# compute signature and compare signatures
signature = base64.encodestring(hmac.new(self.auth_token, s, sha1).\
digest()).strip()
_request.add_header("X-PLIVO-SIGNATURE", "%s" % signature)
return _request
评论列表
文章目录