def set(self, tx_hash, value): """设置数据""" key = self.build_key(tx_hash) value = json.dumps(value) G_LOGGER.debug("设置key: {} 值: {}".format(key, value)) return self.client.set(key, value)
def processor(data): """JSONRPC协议中, result与error互斥, 两者不可能同时拥有值.""" results = [] for d in data: if not isinstance(d, dict): continue results.append(d.get('result')) if ignore_err: return results if not all(results): G_LOGGER.debug('many post中返回数据错误: {}'.format(data)) raise JsonRpcError(-1, "many post中返回数据中错误.")
def gen_address_p2sh(self, script_bytes): """ p2sh根据验证信息解析出reeddemscript, reeddemscript--->address param: magicbyte 版本前缀 """ magicbyte, magicbyte_length = self.get_address_magicbyte(address_type='P2SH') curosr = 1 while curosr < len(script_bytes): n, length = self.get_curosr_vrant(script_bytes, curosr) curosr += n content = script_bytes[curosr: curosr + length] curosr += length address = self.p2sh_scriptaddr(content.hex(), magicbyte=magicbyte, magicbyte_length=magicbyte_length) G_LOGGER.debug("{} ---> P2PKH Address: {}".format(self.coin_type, address)) return address
def gen_address_p2pk(self, script_bytes): """ 根据p2pkh的签名信息解析出pubkey,pubkey--->address param: magicbyte 版本前缀 """ magicbyte, magicbyte_length = self.get_address_magicbyte(address_type='P2PKH') curosr = 0 while curosr < len(script_bytes): n, length = self.get_curosr_vrant(script_bytes, curosr) curosr += n content = script_bytes[curosr: curosr + length] curosr += length address = self.pubkey_to_address(content, magicbyte=magicbyte, magicbyte_length=magicbyte_length) G_LOGGER.debug("{} ---> P2PKH Address: {}".format(self.coin_type, address)) return address
def sismember(self, address, value): key = self.build_key(address) is_mem = self.client.sismember(key, value) G_LOGGER.debug('是否为用户元素 -- 用户: {} 元素: {} 结果: {}'.format( key, value, is_mem)) return is_mem
def srem(self, address, *values): # 删除一个地址的中tx_hash key = self.build_key(address) rm = self.client.srem(key, *values) G_LOGGER.debug('删除用户元素 -- 用户: {} 元素: {}'.format(key, values)) return rm
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))