def validate_sms(phone_num, validate_code): """验证用户输入的短信验证码是否正确""" message = {"message": "success"} key = "sms_{}".format(phone_num) raw_code = cache.get(key) if raw_code is None: # message['message'] = "验证码不能为None" message = pack_message(message, error_code=3000, tel_verify_code=raw_code) elif int(validate_code) != raw_code: # message['message'] = "验证码错误" message = pack_message(message, error_code=3002, tel_verify_code=raw_code) else: pass return message
def send_sms(phone_num): """发送短信并检查短信发送的请求是否合法,用户发送短信时请调用次方法""" """一天最多允许发送10次短信""" max_time = 10 # 24小时内的最大短信发送次数 interval = 60 # 短信发送间隔 message = {"message": "success"} key = "sms_send_time_{}".format(phone_num) # 用于计数的key的前缀 recode_list = list() for i in range(max_time): sub_key = "{}_{}".format(key, str(i)) val = cache.get(sub_key) if val is None: break else: recode_list.append(val) if len(recode_list) == 0: message = __send_sms(phone_num) if message['message'] == "success": """计数""" temp_key = "{}_{}".format(key, str(len(recode_list))) cache.set(temp_key, datetime.datetime.now(), timeout=60 * 24) elif 0 < len(recode_list) < max_time: now = datetime.datetime.now() prev = recode_list[-1] seconds = (now - prev).total_seconds() """检查发送间隔""" if seconds < interval: # message['message'] = "短信发送频繁,请等待{}秒后再试".format(int(interval - seconds)) message = pack_message(message, error_code=4190, delay_seconds=int(interval - seconds)) else: message = __send_sms(phone_num) if message['message'] == "success": """计数""" temp_key = "{}_{}".format(key, str(len(recode_list))) cache.set(temp_key, datetime.datetime.now(), timeout=60 * 24) else: # message['message'] = "已超本日短信最大发送次数" message = pack_message(message, error_code=4014, count=len(recode_list)) return message
def validate_sms(phone_num, validate_code): """验证用户输入的短信验证码是否正确""" message = {"message": "success"} key = "sms_{}".format(phone_num) raw_code = cache.get(key) if raw_code is None: message['message'] = "验证失败" elif isinstance(validate_code, int): if validate_code != raw_code: message['message'] = "验证码错误" else: pass elif isinstance(validate_code, str) and validate_code.isdigit(): if int(validate_code) != raw_code: message['message'] = "验证码错误" else: pass else: message['message'] = "未知错误" return message
def check_version(os_type="android"): """检查apk版本 os_type 移动端操作系统 return 字典 """ sub_dir_name = "apk" if os_type.lower() == "android": pass else: try: ms = "check_version func Error,os_type={}".format(os_type) raise ValueError(ms) except ValueError as e: print(e) logger.exception("Error:e={}".format(e)) key = "{}_check_version".format(sub_dir_name) result = cache.get(key) if result is None: result = check_file(sub_dir_name) cache.set(key, result, timeout=60) return result
def query_geo_coordinate(**kwargs): """ 查询经纬度的接口,和上面的方法不同,此方法直接暴露给查询接口 :param arg:city 城市名 :param kwargs:address 地址字符串 :return: doc或者none """ city = kwargs['city'] address = kwargs['address'] key = "query_geo_coordinate_{}_{}".format(city, address) # 缓存标识 if cache.get(key) is None: position_data, real = get_position_by_address(city=city, address_str=address) cache.delete(key) args = { "address": address, "city": city, "real_value": real, "longitude": position_data[0], "latitude": position_data[1] } return args else: return None
def __init__(self): key = "amap_adcode" ad_codes = cache.get(key) if ad_codes is None: pass
def check_server_and_send_mail(*args, **kwargs): """ 检查副本集合的服务器是否在线?如果不在线的话就发送email.(只有在状态改变的时候才发生送email) :param args: :param kwargs: :return: """ """ status_dict是存放上一次服务器探测结果的字典,以服务器的 ip+":"+port为key,相关信息字典为value { "192.168.0.110:27017": {"ip":"192.168.0.110","port":"27017", "last_time":"2017-11-27 10:11:21.940", "last_status": True}, .... } """ key = "replica_set_status" status_dict = {} if cache.get(key) is None else cache.get(key) for host in replica_hosts: current_status = False ip = host['host'] port = host['port'] try: t = telnetlib.Telnet(host=ip, port=port, timeout=15) current_status = True t.close() except Exception as e: mes = "{}:{}连接失败,错误原因:{}".format(ip, port, e) logger.exception(mes) print(e) finally: now = datetime.datetime.now() temp_key = "{}:{}".format(ip, port) temp_value = { "ip": ip, "port": port, "last_time": now, "last_status": current_status } prev_status = None if len(status_dict) == 0: pass else: try: prev_status = status_dict[temp_key]['last_status'] except KeyError as e: print(e) except TypeError as e: print(e) finally: pass flag_mail = False status_dict[temp_key] = temp_value cache.set(key, status_dict, timeout=60 * 5) if prev_status is None and not current_status: """第一次就检测服务器失败""" flag_mail = True elif prev_status is not None and prev_status != current_status: flag_mail = True else: pass if flag_mail: title = "{} 服务器mongodb检测{}".format( ip, "正常" if current_status else "失败") content = "{} 服务器ip:{},mongodb例行检查结果:{}".format( now, ip, current_status) send_mail(title=title, content=content) else: pass ms = "check_server_and_send_mail 函数检测结果: {}".format(status_dict) logger.info(ms) # celery有特殊的日志系统,传统的做法无效