def logger(self,name): collector = name collect_level = config.get_str('log','collect_level') output_level = config.get_str('log','output_level') fmt = config.get_str('log','fmt') output = config.get_str('log','output') my_log = logging.getLogger(collector)#定义一个日志收集器 my_log.setLevel(collect_level)#设置收集日志的级别 #设置日志的输出渠道,输出到控制台或者指定文件 if output == 'console': ch = logging.StreamHandler() else: # 定义一个一天换一次日志文件的渠道(每天生成一个新的日志文件),最多保留两个旧的日志文件,也就是说保留两天的日志文件 ch = TimedRotatingFileHandler(log_dir+output, when='D', interval=1, backupCount=2,encoding='utf-8') ch.setLevel(output_level)#设置输出日志的级别 my_log.addHandler(ch)#给日志添加输出渠道 formatter = logging.Formatter(fmt)#设置日志输出的格式 ch.setFormatter(formatter) return my_log
def __init__(self): host = config.get_str('db', 'host') port = config.get_int('db', 'port') user = config.get_str('db', 'user') pwd = config.get_str('db', 'pwd') self.mysql = pymysql.connect(host=host, port=port, user=user, password=pwd) self.cursor = self.mysql.cursor( pymysql.cursors.DictCursor) #返回的数据类型是字典
def __init__(self): host = config.get_str('db', 'host') user = config.get_str('db', 'user') pwd = config.get_str('db', 'pwd') port = config.get_int('db', 'port') self.mysql = pymysql.connect(host=host, user=user, password=pwd, port=port) # 1.新建数据库连接 # self.cursor = self.mysql.cursor() # 2.新建查询,这种方式查询返回的数据类型是元组 self.cursor = self.mysql.cursor( pymysql.cursors.DictCursor) #这种方式查询返回的数据是字典
def request(self, method, url, data=None, json=None, request_cookies=None): #传参:请求方法,接口地址,请求参数,请求cookies if type(data) == str: #将data转换为字典 data = eval(data) url = config.get_str( 'api', 'pre_url') + url #读取配置文件中的接口地址,再将其与Excel中的url拼接,形成最终的请求地址 if method.upper() == 'GET': res = requests.get(url, params=data, cookies=request_cookies) elif method.upper() == 'POST': if json: res = requests.post(url, json=json, cookies=request_cookies) else: res = requests.post(url, data=data, cookies=request_cookies) else: res = None print('UN-support method') return res
def logger(self,name): collect_level = config.get_str('log','collect_level') output_level = config.get_str('log','output_level') fmt = config.get_str('log','fmt') output = config.get_str('log','output') logger = logging.getLogger(name) logger.setLevel(collect_level) if output == 'console': ch = logging.StreamHandler() else: ch = TimedRotatingFileHandler(log_dir+output, when='D', interval=1, backupCount=2, encoding='utf-8') ch.setLevel(output_level) logger.addHandler(ch) formatter = logging.Formatter(fmt) ch.setFormatter(formatter) return logger
def request(self, url, data): url = config.get_str('api', 'pre_url') + url client = Client(url) if self.api == 'sendMCode': result = client.service.sendMCode(data) elif self.api == 'userRegister': result = client.service.userRegister(data) elif self.api == 'verifiedUserAuth': result = client.service.verifyUserAuth(data) elif self.api == 'bindBankCard': result = client.service.bindBankCard(data) return result
def replace(self, data): """使用正则表达式,将Excel中参数化的值替换为配置文件中读取出来的值""" p = '#(.*?)#' while re.search(p, data): #判断data中能否根据正则表达式找到匹配的值 m = re.search(p, data) # 从任意位置开始找,找第一个就返回Match object, 如果没有找到返回None g = m.group(1) #找到参数化的key值 try: v = config.get_str('testdata', g) #根据key去配置文件里面取值 except configparser.NoOptionError as e: if hasattr(Context, g): v = getattr(Context, g) else: print('找不到值') raise e data = re.sub(p, v, data, count=1) #查找并替换Excel中参数化的值 return data
def replace(self, data): p = '#(.*?)#' while re.search(p, data): m = re.search(p, data) g = m.group(1) try: v = config.get_str('testdata', g) except configparser.NoOptionError as e: if hasattr(Context, g): v = getattr(Context, g) else: print('找不到值') raise e data = re.sub(p, v, data, count=1) return data
def request(self, method, url, data=None, json=None): if type(data) == str: data = eval(data) url = config.get_str( 'api', 'pre_url') + url #读取配置文件中的接口地址,再将其与Excel中的url拼接,形成最终的请求地址 if method.upper() == 'GET': res = self.session.request(method=method, url=url, params=data) elif method.upper() == 'POST': if json: res = self.session.request(method=method, url=url, json=json) else: res = self.session.request(method=method, url=url, data=data) else: res = None print('Un-support method') return res