def check_parameter(action, *keys): if type(action) is str: try: api_logger.debug("准备检查参数格式") json_action = json.loads(action) except Exception as e: api_logger.warn("不能转为json格式,原因:" + repr(e)) return False else: # 检查必传项目 for key in keys: # 如果缺少必传项 if key[0] not in json_action: return False else: value = json_action[key[0]] # 先检查类型 if type(value) is not key[1]: return False # 如果非布尔型,则检查长度 if key[1] is not bool: # 如果有最小值但没最大值 if key[2] and not key[3]: if key[1] is str or key[1] is list: # 如果比最小值还小 if len(value) < key[2]: return False elif key[1] is int: # 如果比最小值还小 if value < key[2]: return False # 如果有最小以及最大值 elif key[2] and key[3]: if key[1] is str or key[1] is list: # 如果比最小值还小或者比最大值还大 if len(value) < key[2] or len(value) > key[3]: return False elif key[1] is int: # 如果比最小值还小或者比最大值还大 if value < key[2] or value > key[3]: return False # 如果有没有最小值但有最大值 elif not key[2] and key[3]: if key[1] is str or key[1] is list: # 如果比最大值还大 if len(value) > key[3]: return False elif key[1] is int: # 如果比最大值还大 if value > key[3]: return False return json_action else: return False
def task_running_result_report(ws): while not ws.closed: url = 'taskRunningResultReport.socket' api_logger.debug('监听来自客户端的首次请求内容...') re_data = ws.receive() api_logger.debug('接收到来自客户端的首次请求内容') # 校验传参 re_json_data = check_parameter(re_data, ['mail', str, None, None], ['token', str, None, None], ['taskId', int, None, None], ['search', int, 1, 3], ['per', int, None, None]) if not re_json_data: api_logger.debug('必传参数检查失败') ws.send('fail') ws.close() else: api_logger.debug('必传参数检查成功') re_task_id = re_json_data['taskId'] re_search = re_json_data['search'] re_log_per = re_json_data['per'] # 校验token if not check_token(re_json_data['mail'], re_json_data['token']): api_logger.error('测试任务:%stoken检查失败' % re_task_id) ws.send('fail') ws.close() else: api_logger.debug('测试任务:%stoken检查成功' % re_task_id) # 校验账户状态 if not check_user(re_json_data['mail']): api_logger.error('测试任务:%s账户状态检查失败' % re_task_id) ws.send('fail') ws.close() else: api_logger.debug('测试任务:%s账户状态检查成功' % re_task_id) # 校验权限 if not check_auth(re_json_data['mail'], url): api_logger.error('测试任务:%s账户权限校验失败' % re_task_id) ws.send('fail') ws.close() else: api_logger.debug('测试任务:%s账户权限校验成功' % re_task_id) # 获取测试计划内容快照 # 本方法返回值固定故仅执行一次,结果后续都可以用 result_flag, snap_data = get_snap_data(re_task_id) if not result_flag: api_logger.error('测试任务:%s测试插件数据获取失败' % re_task_id) ws.send('fail') ws.close() elif snap_data is None: api_logger.error('测试任务:%s测试插件数据为空' % re_task_id) ws.send('fail') ws.close() else: api_logger.debug('测试任务:%s测试插件数据获取成功' % re_task_id) # 首次返回 result_flag, result_info = get_running_result(re_task_id, snap_data, re_search, 0, re_log_per) if result_flag: api_logger.debug('测试任务:%s运行日志获取成功' % re_task_id) api_logger.debug('测试任务:%s准备回传首次内容' % re_task_id) ws.send(result_info) api_logger.debug('测试任务:%s首次内容回传成功' % re_task_id) get_flag = True while get_flag: api_logger.debug('测试任务:%s监听来自客户端的请求...' % re_task_id) re_data = ws.receive() api_logger.debug('测试任务:%s接收到来自客户端的请求内容' % re_task_id) re_json_data = check_parameter(re_data, ['action', str, None, None]) if not re_json_data: api_logger.error('测试任务:%s轮询时接收数据的必传参数检查失败' % re_task_id) ws.send('fail') get_flag = False else: api_logger.debug('测试任务:%s轮询时接收数据的必传参数检查成功' % re_task_id) re_action = re_json_data['action'] if re_action == 'get': api_logger.error('测试任务:%s客户端请求轮询数据' % re_task_id) re_json_data = check_parameter( re_data, ['offset', int, None, None]) if not re_json_data: api_logger.error('测试任务:%s轮询时接收数据的必传参数检查失败' % re_task_id) ws.send('fail') get_flag = False else: api_logger.debug('测试任务:%s轮询时接收数据的必传参数检查成功' % re_task_id) re_offset = re_json_data['offset'] result_flag, result_info = get_running_result( re_task_id, snap_data, re_search, re_offset, re_log_per) if result_flag: api_logger.debug('测试任务:%s轮询时待返回数据获取成功' % re_task_id) api_logger.debug('测试任务:%s轮询时准备回传轮询内容' % re_task_id) ws.send(result_info) api_logger.debug('测试任务:%s轮询时轮询内容回传成功' % re_task_id) else: ws.send('fail') api_logger.error('测试任务:%s轮询时待返回数据获取失败' % re_task_id) get_flag = False elif re_action == 'close': api_logger.error('测试任务:%s客户端请求关闭连接' % re_task_id) ws.send('bye') get_flag = False else: api_logger.warn('测试任务:%s客户端请求方法暂不支持' % re_task_id) ws.send('bye') get_flag = False api_logger.debug('测试任务:%s关闭请求' % re_task_id) ws.close() else: api_logger.error('测试任务:%s运行日志获取失败' % re_task_id) ws.send('fail') ws.close()
def time_consuming_analysis(ws): while not ws.closed: url = 'timeConsumingAnalysis.socket' api_logger.debug('监听来自客户端的首次请求内容...') re_data = ws.receive() api_logger.debug('接收到来自客户端的首次请求内容') # 校验传参 re_json_data = check_parameter( re_data, ['mail', str, None, None], ['token', str, None, None], ['taskId', int, None, None], ['interval', int, None, None], ['type', int, 0, None] ) if not re_json_data: api_logger.debug('必传参数检查失败') ws.send('fail') ws.close() return False else: api_logger.debug('必传参数检查成功') re_mail = re_json_data['mail'] re_token = re_json_data['token'] re_task_id = re_json_data['taskId'] re_interval = re_json_data['interval'] re_type = re_json_data['type'] re_last = 0 re_start_time = '' re_start_time_stamp = 0 re_end_time = '' re_end_time_stamp = 0 # 校验token if not check_token(re_mail, re_token): api_logger.warn('测试任务:%stoken检查失败' % re_task_id) ws.send('fail') ws.close() return False else: api_logger.debug('测试任务:%stoken检查成功' % re_task_id) # 校验账户状态 if not check_user(re_json_data['mail']): api_logger.warn('测试任务:%s账户状态检查失败' % re_task_id) ws.send('fail') ws.close() return False else: api_logger.debug('测试任务:%s账户状态检查成功' % re_task_id) # 校验权限 if not check_auth(re_json_data['mail'], url): api_logger.warn('测试任务:%s账户权限校验失败' % re_task_id) ws.send('fail') ws.close() return False else: api_logger.debug('测试任务:%s账户权限校验成功' % re_task_id) # 根据type继续检查传参 if re_type in [0, 1, 2, 3]: re_last = {0: 30, 1: 60, 2: 300, 3: 1800}[re_type] # 校验传参 re_json_data = check_parameter( re_data, ['last', int, 30, None] ) if not re_json_data: api_logger.debug('必传参数检查失败') ws.send('fail') ws.close() return False else: api_logger.debug('必传参数检查成功') result_flag, result_data = get_the_last_log_finish_time(re_task_id) if not result_flag: api_logger.error('测试任务:%s末条运行日志数据获取失败' % re_task_id) ws.send('fail') ws.close() return False else: api_logger.debug('测试任务:%s末条运行日志数据获取成功' % re_task_id) # 获取任务最后一条log的完成时间获得时间间隔的右边界 re_end_time_stamp = result_data # 倒推获得时间间隔的左边界 re_start_time_stamp = re_end_time_stamp - re_last * 1000 else: re_json_data = check_parameter( re_data, ['start', str, None, None], ['end', str, None, None] ) if not re_json_data: api_logger.debug('必传参数检查失败') ws.send('fail') ws.close() return False else: api_logger.debug('必传参数检查成功') # 校验开始和结束时间 try: re_start_time = re_json_data['start'] re_end_time = re_json_data['end'] re_start_time_stamp = int(time.mktime(time.strptime(re_start_time, "%Y-%m-%d %H:%M:%S"))) * 1000 re_end_time_stamp = int(time.mktime(time.strptime(re_end_time, "%Y-%m-%d %H:%M:%S"))) * 1000 except Exception as e: api_logger.warn('测试任务:%s时间参数传递非法:%s' % (re_task_id, repr(e))) ws.send('fail') ws.close() return False else: if re_start_time_stamp >= re_end_time_stamp: api_logger.warn('测试任务:%s时间参数传递非法:开始日期大于等于结束日期' % re_task_id) ws.send('fail') ws.close() return False else: api_logger.debug('测试任务:%s时间参数传递合法' % re_task_id) # 获取测试计划内容快照 # 本方法返回值固定故仅执行一次,结果后续都可以用 result_flag, snap_data = get_snap_data(re_task_id) if not result_flag: api_logger.error('测试任务:%s测试插件数据获取失败' % re_task_id) ws.send('fail') ws.close() return False elif snap_data is None: api_logger.warn('测试任务:%s测试插件数据为空' % re_task_id) ws.send('fail') ws.close() return False else: api_logger.debug('测试任务:%s测试插件数据获取成功' % re_task_id) # 首次返回 result_flag, result_info = get_running_result( re_task_id, snap_data, interval=re_interval, start=re_start_time_stamp, end=re_end_time_stamp ) if result_flag: api_logger.debug('测试任务:%s运行日志获取成功' % re_task_id) api_logger.debug('测试任务:%s准备回传首次内容' % re_task_id) ws.send(result_info) api_logger.debug('测试任务:%s首次内容回传成功' % re_task_id) msg = '' while True: api_logger.debug('测试任务:%s监听来自客户端的请求...' % re_task_id) re_data = ws.receive() api_logger.debug('测试任务:%s接收到来自客户端的请求内容' % re_task_id) re_json_data = check_parameter( re_data, ['action', str, None, None] ) if not re_json_data: api_logger.error('测试任务:%s轮询时接收数据的必传参数检查失败' % re_task_id) break else: api_logger.debug('测试任务:%s轮询时接收数据的必传参数检查成功' % re_task_id) re_action = re_json_data['action'] if re_action == 'get': api_logger.error('测试任务:%s客户端请求轮询数据' % re_task_id) re_json_data = check_parameter( re_data, ['interval', int, None, None], ['type', int, None, None] ) if not re_json_data: api_logger.error('测试任务:%s轮询时接收数据的必传参数检查失败' % re_task_id) break else: api_logger.debug('测试任务:%s轮询时接收数据的必传参数检查成功' % re_task_id) re_interval = re_json_data['interval'] re_type = re_json_data['type'] re_last = 0 re_start_time = '' re_start_time_stamp = 0 re_end_time = '' re_end_time_stamp = 0 # 根据type继续检查传参 if re_type in [0, 1, 2, 3]: re_last = {0: 30, 1: 60, 2: 300, 3: 1800}[re_type] # 校验传参 re_json_data = check_parameter( re_data, ['last', int, 30, None] ) if not re_json_data: api_logger.error('测试任务:%s轮询时必传参数检查失败' % re_task_id) break else: api_logger.debug('测试任务:%s轮询时必传参数检查成功' % re_task_id) result_flag, result_data = get_the_last_log_finish_time(re_task_id) if not result_flag: api_logger.error('测试任务:%s末条运行日志数据获取失败' % re_task_id) break else: api_logger.debug('测试任务:%s末条运行日志数据获取成功' % re_task_id) # 获取任务最后一条log的完成时间获得时间间隔的右边界 re_end_time_stamp = result_data # 倒推获得时间间隔的左边界 re_start_time_stamp = re_end_time_stamp - re_last * 1000 else: re_json_data = check_parameter( re_data, ['start', str, None, None], ['end', str, None, None] ) if not re_json_data: api_logger.debug('测试任务:%s轮询时必传参数检查失败' % re_task_id) break else: api_logger.debug('测试任务:%s轮询时必传参数检查成功' % re_task_id) # 校验开始和结束时间 try: re_start_time_stamp = int( time.mktime(time.strptime(re_start_time, "%Y-%m-%d %H:%M:%S"))) re_end_time_stamp = int(time.mktime(time.strptime(re_end_time, "%Y-%m-%d %H:%M:%S"))) except Exception as e: api_logger.warn('测试任务:%s时间参数传递非法:%s' % (re_task_id, repr(e))) break else: if re_start_time_stamp >= re_end_time_stamp: api_logger.warn('测试任务:%s时间参数传递非法:开始日期大于等于结束日期' % re_task_id) break else: api_logger.debug('测试任务:%s时间参数传递合法' % re_task_id) result_flag, result_info = get_running_result( re_task_id, snap_data, interval=re_interval, start=re_start_time_stamp, end=re_end_time_stamp ) if result_flag: api_logger.debug('测试任务:%s轮询时待返回数据获取成功' % re_task_id) api_logger.debug('测试任务:%s轮询时准备回传轮询内容' % re_task_id) ws.send(result_info) api_logger.debug('测试任务:%s轮询时轮询内容回传成功' % re_task_id) else: api_logger.error('测试任务:%s轮询时待返回数据获取失败' % re_task_id) break elif re_action == 'close': api_logger.debug('测试任务:%s客户端请求关闭连接' % re_task_id) break else: api_logger.warn('测试任务:%s客户端请求方法暂不支持' % re_task_id) break api_logger.debug('测试任务:%s关闭请求' % re_task_id) ws.send('fail') ws.close() return False else: api_logger.error('测试任务:%s运行日志获取失败' % re_task_id) ws.send('fail') ws.close() return False
def wrapper(*args, **kwargs): if flask.request.method == "POST": # 检查files api_logger.debug("准备检查请求内的文件清单") try: request_files = flask.request.files except Exception as e: api_logger.warn("文件清单检查失败,原因:" + repr(e)) return error_msgs[301]['msg_request_params_illegal'] else: if not request_files: return error_msgs[301]['msg_request_params_illegal'] else: # 检查必传项目 check_files = keys[0]['files'] for check_file in check_files: # 如果缺少必传项 if check_file[0] not in request_files: return error_msgs[302][ 'msg_request_params_incomplete'] else: request_file = request_files[check_file[0]] request_file.seek(0, os.SEEK_END) request_file_size = request_file.tell() # 检查大小 if request_file_size < check_file[ 2] or request_file_size > check_file[3]: return error_msgs[201][ 'msg_request_file_oversize'] request_file.seek(0, 0) # 检查forms api_logger.debug("准备检查请求内的参数") try: request_forms = flask.request.form except Exception as e: api_logger.warn("请求内的参数检查失败,原因:" + repr(e)) return error_msgs[301]['msg_request_params_illegal'] else: if not request_forms: return error_msgs[301]['msg_request_params_illegal'] else: # 检查必传项目 check_forms = keys[0]['forms'] for check_form in check_forms: # 如果缺少必传项 if check_form[0] not in request_forms: return error_msgs[302][ 'msg_request_params_incomplete'] else: request_form = request_forms[check_form[0]] # 先检查类型 # 由于form类型的值传递时实际都为字符串,故要先尝试转换 try: request_form_trans = check_form[1]( request_form) except Exception as e: api_logger.warn("表单数据转换失败,原因:" + repr(e)) return error_msgs[301][ 'msg_request_params_illegal'] else: check_flag = True # 如果bool # 不检查 if check_form[1] is bool: pass # 如果int if check_form[1] is int: # 如果有最小值限定 if check_form[ 2] and request_form_trans < check_form[ 2]: check_flag = False # 如果有最大值限定 if check_form[ 3] and request_form_trans > check_form[ 3]: check_flag = False # 判断失败 if not check_flag: return error_msgs[301][ 'msg_request_params_illegal'] # 如果str if check_form[1] is str: # 如果有最小值限定 if check_form[2] and len( request_form_trans ) < check_form[2]: check_flag = False # 如果有最大值限定 if check_form[3] and len( request_form_trans ) > check_form[3]: check_flag = False # 判断失败 if not check_flag: return error_msgs[301][ 'msg_request_params_illegal'] return func(*args, **kwargs)
def requestfail_value(cls, msg): api_logger.warn("参数传值非法:" + msg) response_json = {"code": 301, "msg": "参数传值非法", "data": {}} return response_json
def requestfail_server(cls, msg): api_logger.warn("服务处理异常:" + msg) response_json = {"code": 500, "msg": "服务处理异常", "data": {}} return response_json
def requestfail_nokey(cls, msg): api_logger.warn("缺少必传项:" + msg) response_json = {"code": 302, "msg": "缺少必传参数", "data": {}} return response_json
def requestfail_error(cls, msg): api_logger.warn("请求操作失败:" + msg) response_json = {"code": 201, "msg": msg, "data": {}} return response_json