helpers.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:YouPBX 作者: JoneXiong 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号