# Path: qingcloud/conn/auth.py
# class QuerySignatureAuthHandler(HmacKeys):
#     """ Provides Query Signature Authentication.
#     """
# 
#     SignatureVersion = 1
#     APIVersion = 1
# 
#     def _calc_signature(self, params, verb, path):
#         """ calc signature for request
#         """
#         string_to_sign = '%s\n%s\n' % (verb, path)
#         params['signature_method'] = self.algorithm()
#         keys = sorted(params.keys())
#         pairs = []
#         for key in keys:
#             val = get_utf8_value(params[key])
#             if is_python3:
#                 key = key.encode()
#             pairs.append(urllib.quote(key, safe='') + '=' +
#                          urllib.quote(val, safe='-_~'))
#         qs = '&'.join(pairs)
#         string_to_sign += qs
#         # print "string to sign:[%s]" % string_to_sign
#         b64 = self.sign_string(string_to_sign)
#         return (qs, b64)
# 
#     def add_auth(self, req, **kwargs):
#         """ add authorize information for request
#         """
#         req.params['access_key_id'] = self.qy_access_key_id if 'access_key' not in kwargs else kwargs.get('access_key')
#         if 'token' in kwargs:
#             req.params['token'] = kwargs.get('token')
#         req.params['signature_version'] = self.SignatureVersion if 'signature_version' not in kwargs \
#             else kwargs.get('signature_version')
#         req.params['version'] = self.APIVersion
#         time_stamp = get_ts()
#         req.params['time_stamp'] = time_stamp
#         qs, signature = self._calc_signature(req.params, req.method,
#                                              req.auth_path)
#         # print 'query_string: %s Signature: %s' % (qs, signature)
#         if req.method == 'POST':
#             # req and retried req should not have signature
#             params = req.params.copy()
#             params["signature"] = signature
#             req.body = urllib.urlencode(params)
#             req.header = {
#                 'Content-Length': str(len(req.body)),
#                 'Content-Type': 'application/x-www-form-urlencoded',
#                 'Accept': 'text/plain',
#                 'Connection': 'Keep-Alive'
#             }
#         else:
#             req.body = ''
#             # if this is a retried req, the qs from the previous try will
#             # already be there, we need to get rid of that and rebuild it
#             req.params["signature"] = signature
#             req.path = req.path.split('?')[0]
#             req.path = (req.path + '?' + qs +
#                         '&signature=' + urllib.quote_plus(signature))


import time
import threading
    import httplib
    import http.client as httplib
from qingcloud.misc.json_tool import json_load
from qingcloud.conn.auth import QuerySignatureAuthHandler

        # Send the request
        conn.request(method, request_path, request.body, request.header)

        # Receive the response
        response = conn.getresponse()

        # Reuse the connection
        if response.status < 500:
            self._set_conn(conn)

        return response

    def _check_token(self):
        if not self._token or not self._token_exp or time.time() >= self._token_exp:
            try:
                conn = httplib.HTTPConnection(self.credential_proxy_host, self.credential_proxy_port, timeout=1)
                conn.request("GET", "/latest/meta-data/security-credentials", headers={"Accept": "application/json"})
                response = conn.getresponse()
                # Reuse the connection
                if response.status == 200:
                    r = response.read()
                    if r:
                        # first reverse escape, then json_load
                        r = json_load(eval(r))
                        self._token = r.get('id_token')
                        self._token_exp = r.get('expiration')
                        self.iam_access_key = r.get('access_key')
                        self.iam_secret_key = r.get('secret_key')


# Path: qingcloud/conn/auth.py
# class QuerySignatureAuthHandler(HmacKeys):
#     """ Provides Query Signature Authentication.
#     """
# 
#     SignatureVersion = 1
#     APIVersion = 1
# 
#     def _calc_signature(self, params, verb, path):
#         """ calc signature for request
#         """
#         string_to_sign = '%s\n%s\n' % (verb, path)
#         params['signature_method'] = self.algorithm()
#         keys = sorted(params.keys())
#         pairs = []
#         for key in keys:
#             val = get_utf8_value(params[key])
#             if is_python3:
#                 key = key.encode()
#             pairs.append(urllib.quote(key, safe='') + '=' +
#                          urllib.quote(val, safe='-_~'))
#         qs = '&'.join(pairs)
#         string_to_sign += qs
#         # print "string to sign:[%s]" % string_to_sign
#         b64 = self.sign_string(string_to_sign)
#         return (qs, b64)
# 
#     def add_auth(self, req, **kwargs):
#         """ add authorize information for request
#         """
#         req.params['access_key_id'] = self.qy_access_key_id if 'access_key' not in kwargs else kwargs.get('access_key')
#         if 'token' in kwargs:
#             req.params['token'] = kwargs.get('token')
#         req.params['signature_version'] = self.SignatureVersion if 'signature_version' not in kwargs \
#             else kwargs.get('signature_version')
#         req.params['version'] = self.APIVersion
#         time_stamp = get_ts()
#         req.params['time_stamp'] = time_stamp
#         qs, signature = self._calc_signature(req.params, req.method,
#                                              req.auth_path)
#         # print 'query_string: %s Signature: %s' % (qs, signature)
#         if req.method == 'POST':
#             # req and retried req should not have signature
#             params = req.params.copy()
#             params["signature"] = signature
#             req.body = urllib.urlencode(params)
#             req.header = {
#                 'Content-Length': str(len(req.body)),
#                 'Content-Type': 'application/x-www-form-urlencoded',
#                 'Accept': 'text/plain',
#                 'Connection': 'Keep-Alive'
#             }
#         else:
#             req.body = ''
#             # if this is a retried req, the qs from the previous try will
#             # already be there, we need to get rid of that and rebuild it
#             req.params["signature"] = signature
#             req.path = req.path.split('?')[0]
#             req.path = (req.path + '?' + qs +
#                         '&signature=' + urllib.quote_plus(signature))


# Path: qingcloud/conn/connection.py
import time
import threading
    import httplib
    import http.client as httplib
from qingcloud.misc.json_tool import json_load
from qingcloud.conn.auth import QuerySignatureAuthHandler


        # Send the request
        conn.request(method, request_path, request.body, request.header)

        # Receive the response
        response = conn.getresponse()

        # Reuse the connection
        if response.status < 500:
            self._set_conn(conn)

        return response

    def _check_token(self):
        if not self._token or not self._token_exp or time.time() >= self._token_exp:
            try:
                conn = httplib.HTTPConnection(self.credential_proxy_host, self.credential_proxy_port, timeout=1)
                conn.request("GET", "/latest/meta-data/security-credentials", headers={"Accept": "application/json"})
                response = conn.getresponse()
                # Reuse the connection
                if response.status == 200:
                    r = response.read()
                    if r:
                        # first reverse escape, then json_load
                        r = json_load(eval(r))
                        self._token = r.get('id_token')
                        self._token_exp = r.get('expiration')
                        self.iam_access_key = r.get('access_key')
                        self.iam_secret_key = r.get('secret_key')


                        self._auth_handler = QuerySignatureAuthHandler(self.host,