from django_redis import get_redis_connection import json import xmltodict # 封装方法处理从redis中获取接口数据 def get_data_from_redis(redis_key): # 初始化redis redis_conn = get_redis_connection() if redis_conn.exists(redis_key): redis_data = json.loads(redis_conn.get(redis_key)) resp_data = [] resp_result = 0 if redis_data['format'] == 'json': resp = json.loads(redis_data['response_data']) print('json-response', resp) resp_result = resp['res']['status'] if str(resp_result) == '1': resp_data = resp['res']['data'] if redis_data['format'] == 'xml': resp = xmltodict.parse(redis_data['response_data']) print('xml-response', resp) resp_result = resp['root']['status'] if str(resp_result) == '1': resp_data = resp['root']['data'].get('item', resp['root']['data']) if str(resp_result) != '1': return False, redis_data['format'], '请检查接口返回值或手动输入参数:' if len(resp_data) == 0: return False, redis_data['format'], '接口返回数据为空,请手动输入参数:' return True, redis_data['format'], resp_data return False, 'json', '请手动输入参数,或先请求接口:'