def get_logger(name): logger = logging.getLogger(name) logger.setLevel('DEBUG') fmt = logging.Formatter( '[line:%(lineno)d]%(asctime)s-%(levelname)s-%(filename)s-%(name)s-日志信息:%(message)s' ) console_handler = logging.StreamHandler() # 控制台 # 把日志级别放到配置文件里面配置 console_level = config.get('loglevel', 'console_level') # print(console_level) console_handler.setLevel(console_level) console_handler.setFormatter(fmt) file_hadler = logging.FileHandler(contants.log_dir + '/case.log', encoding='utf-8') # 把日志级别放到配置文件里面配置 file_level = config.get('loglevel', 'file_level') # print(file_level) file_hadler.setLevel(file_level) file_hadler.setFormatter(fmt) logger.addHandler(console_handler) logger.addHandler(file_hadler) return logger
def __init__(self): self.host = eval(config.get('mysql', 'host')) # print(type(self.host),self.host) self.user = eval(config.get('mysql', 'user')) # print(type(self.user),self.user) self.password = eval(config.get('mysql', 'password')) # print(type(self.password),self.password) self.port = int(config.get('mysql', 'port')) # print(type(self.port),self.port) self.mysql = pymysql.connect(host=self.host, user=self.user, password=self.password, port=self.port) # print(self.mysql) self.cursor = self.mysql.cursor()
def request(self, method, url, data=None, json=None): method = method.upper() #转换成大写 if type(data) == str: data = eval(data) #拼接请求的url,再传到url=url的后面的url中 url = config.get('api', 'pre_url') + url # print("请求url",url) # print("请求data",data) logger.debug("请求url:{0}".format(url)) logger.debug("请求data:{0}".format(data)) if method == 'GET': resp = self.session.request(method=method, url=url, params=data) elif method == 'POST': if json: resp = self.session.request(method=method, url=url, json=json) else: resp = self.session.request(method=method, url=url, data=data) else: resp = None # print('UN-support method') logger.error('UN-support method') # print("请求response:",resp.text) logger.debug("请求response:{0}".format(resp.text)) return resp
def replace(data): p = "#(.*?)#" while re.search(p, data): print(data) m = re.search(p, data) # 从任意位置开始找,找到第一个就返回 g = m.group(1) # 拿到参数化的KEY v = config.get('data', g) # 根据KEY取配置文件里面的值 # print(v) # 记得替换后的内容,继续用data接受 data = re.sub(p, v, data, count=1) return data
def replace(data): p = "#(.*?)#" # 正则表达式 while re.search(p, data): print(data) m = re.search(p, data) # 从任意位置开始找,找第一个就返回Match object, 如果没有找None g = m.group(1) # 拿到参数化的KEY try: v = config.get('data', g) # 根据KEY取配置文件里面的值 except configparser.NoOptionError as e: # 如果配置文件里面没有的时候,去Context里面取 if hasattr(Context, g): v = getattr(Context, g) else: print('找不到参数化的值') raise e print(v) # 记得替换后的内容,继续用data接收 data = re.sub(p, v, data, count=1) # 查找替换,count查找替换的次数 return data
def request(self, method, url, params=None, data=None, json=None, cookies=None, headers=None): if type(data) == str: data = eval(data) # 拼接请求的url url = config.get('api', 'pre_url') + url logger.debug('请求url:{}'.format(url)) logger.debug('请求的data:{}'.format(data)) if method.lower() == 'get': resp = requests.get(url, params=params, cookies=cookies, headers=headers) # resp是Response对象 elif method.lower() == 'post': if json: # 如果json传参不为空 resp = requests.post(url, json=json, cookies=cookies, headers=headers) else: resp = requests.post(url, data=data, cookies=cookies, headers=headers) else: logger.error('UN-support method') logger.debug('请求response:{}'.format(resp.text)) return resp
def request(self, method, url, data=None, json=None, cookies=None): method = method.upper() #强制转成全大写 if type(data) == str: data = eval(data) # str 转成字典 #拼接请求的url url = config.get('api', 'pre_url') + url if method == 'GET': resp = requests.get(url, params=data, cookies=cookies) # resp 是Response对象 elif method == 'POST': if json: resp = requests.post(url, json=json, cookies=cookies) else: resp = requests.post(url, data=data, cookies=cookies) else: print('UN-support method') return resp
def request(self, method, url, data=None, json=None): method = method.upper() if type(data) == str: data = eval(data) # str 转成字典 # 拼接请求的url url = config.get('api', 'pre_url') + url logger.debug('请求url是:{0}'.format(url)) logger.debug('请求data:{0}'.format(data)) if method == 'GET': resp = self.session.request(method=method, url=url, params=data) elif method == 'POST': if json: resp = self.session.request(method=method, url=url, json=json) else: resp = self.session.request(method=method, url=url, data=data) else: resp = None logger.error('UN-support method') logger.debug('请求结果是:{0}'.format(resp.text)) return resp
""" import re from API_8.common.config import config data = '{"mobilephone":"#normal_user#","pwd":"#normal_pwd#"}' # 原本字符,元字符 p = "#(.*?)#" # 正则表达式 # ms = re.findall(p, data) # 查找全部,返回列表 # print(ms) # m = re.search(p, data) # 从任意位置开始找,找第一个就返回Match object, 如果没有找None # print(m.group(0)) # 返回表达式和组里面的内容 # print(m.group(1)) # 只返回指定组的内容 # g = m.group(1) # 拿到参数化的KEY # v = config.get('data', g) # 根据KEY取配置文件里面的值 # print(v) # data_new = re.sub(p, v, data, count=1) # 查找替换,count查找替换的次数 # print(data_new) # 如果要匹配多次,替换多次,使用循环来解决 while re.search(p, data): print(data) m = re.search(p, data) # 从任意位置开始找,找第一个就返回Match object, 如果没有找None g = m.group(1) # 拿到参数化的KEY v = config.get('data', g) # 根据KEY取配置文件里面的值 print(v) # 记得替换后的内容,继续用data接收 data = re.sub(p, v, data, count=1) # 查找替换,count查找替换的次数 print('最后替换后的data', data)