示例#1
0
 def _get(self, params, processor):
     """JSONRPC不支持get请求, 如果使用, 将强转为post请求, 并发出一条警告."""
     G_LOGGER.warn('JsonRpc不支持GET请求, 自动转为post请求, 建议所有使用get请求链接转为post请求!')
     return self._post(params=params, processor=processor)
示例#2
0
 def _send_data(self, method, url, _params=None, _data=None, _json=None, _cookies=None):
     data = _params or _data or _json
     G_LOGGER.debug('接口: {}, 方法: {}, 请求数据: {}'.format(url, method, data))
     t1 = time.time()
     retry_time = 0.001
     while True:
         try:
             with self.session.request(method, url, params=_params, data=_data, json=_json, auth=self.auth,
                                       headers=self.headers, timeout=self.timeout, cookies=_cookies) as rsp:
                 G_LOGGER.debug('返回数据: {}'.format(rsp.text))
                 if 300 > rsp.status_code >= 200:
                     result = rsp.text
                     if self.is_json:
                         try:
                             t2 = time.time()
                             net_time = int((t2 - t1) * 1000) / 1000
                             res = json.loads(rsp.text)
                             t3 = time.time()
                             json_time = int((t3 - t2) * 1000) / 1000
                             all_time = int((t3 - t1) * 1000) / 1000
                             # G_LOGGER.info("json success, {}, {}, length={}, net_time={}, json_time={}, all_time={}, ".format(url, data, len(rsp.text), net_time, json_time, all_time))
                             return res
                         except Exception:
                             # G_LOGGER.info("json fail, {}, {}, length={}, net_time={}".format(url, data, len(rsp.text), net_time))
                             pass
                     return result
                 elif rsp.status_code == 404:
                     G_LOGGER.warn("[404] URL不存在, 请检查URL".format(self.auth))
                     raise UrlError("[404] URL不存在, 请检查URL")
                 elif rsp.status_code == 401:
                     G_LOGGER.warn("[401] auth验证错误. auth: {}".format(self.auth))
                     raise RequestError(rsp.status_code, "[401] auth验证错误. auth: {}".format(self.auth))
                 elif rsp.status_code == 403:
                     G_LOGGER.warn("[403] 没有权限操作此URL. url: {}".format(url))
                     raise RequestError(rsp.status_code, "[403] 没有权限操作此URL. url: {}".format(url))
                 elif 500 > rsp.status_code >= 400:
                     G_LOGGER.warn("[{}] 客户端请求错误. 错误详情: {}".format(rsp.status_code, rsp.text))
                     raise RequestError(rsp.status_code,
                                        "[{}] 客户端请求错误. 错误详情: {}".format(rsp.status_code, rsp.text),
                                        rsp)
                 elif 599 > rsp.status_code >= 500:
                     G_LOGGER.warn("[{}] 服务器响应错误. 错误文本: {}".format(rsp.status_code, rsp.text))
                     raise RequestError(rsp.status_code,
                                        "[{}] 服务器响应错误. 错误文本: {}".format(rsp.status_code, rsp.text),
                                        rsp)
         except (UrlError, RequestError):
             raise
         except (request_error.Timeout, request_error.ConnectionError) as e:
             if retry_time < 10:
                 retry_time = min(max(retry_time, retry_time * 2), 10)
             G_LOGGER.info("{}超时或被拒绝, retry_time={}, {} {}, 错误:{}".format(url, retry_time, method, data, str(e)))
             continue
         except request_error.InvalidURL:
             G_LOGGER.error('请求URL无效, 请检查: {}'.format(url))
             raise UrlError('URL无效.')
         except Exception as e:
             G_LOGGER.error('请求出现不可预知异常, 请处理. \r\n'
                               '请求url: {}\r\n'
                               '请求方法: {}\r\n'
                               '请求参数: params: {}, data: {}, json: {}\r\n'
                               '验证用户: {}\r\n'
                               '错误详情: {}'.format(url, method, _params, _data, _json, self.auth,
                                                 traceback.format_exc()))
             raise RequestError(0, "请求出现不可预知异常, 请处理. 详情简要: {}".format(e))