def get_logger(name): logger = logging.getLogger(name) # 名为case_log的日志收集器 logger.setLevel(config.get('log_info', 'collect_level')) # 设定收集的级别 #指定格式 formatter = logging.Formatter(config.get('log_info', 'log_famtter')) # 新建指定的输出渠道: # 指定输出渠道 handler console_handler = logging.StreamHandler() # 指定输出到console控制台 console_handler.setLevel(config.get('log_info', 'output_level')) # 设定输出信息的级别 console_handler.setFormatter(formatter) # 指定输出文本渠道 handler curTime = time.strftime("%Y-%m-%d %H%M", time.localtime()) file_handler = logging.FileHandler(contants.log_dir + "/api_{0}.log".format(curTime), encoding='utf-8') file_handler.setLevel(config.get('log_info', 'output_level')) # 设定输出信息的级别 file_handler.setFormatter(formatter) # 配合关系 logger.addHandler(console_handler) logger.addHandler(file_handler) # 收集日志 return logger # logger = get_logger('case') # logger.info('测试开始啦') # logger.error('测试报错') # logger.debug('测试数据') # logger.info('测试结束')
def __init__(self): #建立连接 host = config.get('mysql','host') user = config.get('mysql','user') password = config.get('mysql','password') port = config.get('mysql','port') self.mysql=pymysql.connect(host=host,user=user,password=password,port=int(port)) self.cursor = self.mysql.cursor(pymysql.cursors.DictCursor) #创建字典格式游标,返回的是字典格式
def http_request(self, method, url, data=None, json=None, headers=None, verify=None): url = config.get('api', 'pre_url') + url if method.lower() == 'get': resp = self.session.request(method=method, url=url, params=data, headers=headers, verify=verify) elif method.lower() == 'post': if json: resp = self.session.request(method=method, url=url, json=data, headers=headers, verify=verify) else: resp = self.session.request(method=method, url=url, data=data, hheaders=headers, verify=verify) else: print('UN-support method') return (resp.text)
def http_request(self,url,data,method,cookies=None,json=None,headers=None,verify=None): ''' :param url: 地址 :param data: 数据 :param method: 请求方式 :param cookies: :param json: 为json格式的时候为True :param headers: 请求头字典格式 :param verify: https格式时为False :return: url = 'https://api.yjq.com/account/anyms/send-vfcode' main = {"phone":"17625188013"} heardes={"Content-Type": "application/json","sign":"1234"} b=HttpsCookies().https_request(url,main,method='post',json=True,headers=heardes) print(b) ''' url=config.get('api','pre_url')+url #url拼接 if method.lower()=='get': resp=requests.get(url=url,params=data,cookies=cookies,headers=headers,verify=verify) elif method.lower()=='post': if json: resp=requests.post(url=url,json=data,cookies=cookies,headers=headers,verify=verify) else: resp=requests.post(url=url,data=data,cookies=cookies,headers=headers,verify=verify) # print ('''响应报文:{} # 响应头:{} # 状态码:{} # 响应cookie:{} # 请求cookies:{}''' # .format(resp.text # ,resp.headers,resp.status_code,resp.cookies,resp.request._cookies)) return (resp.text)
def replace(self,data): while re.search(self.p,data): # print(data) m=re.search(self.p,data) # 从任意位置开始找,找第一个就返回Match object, 如果没有找None g=m.group(1) try: v=config.get('data',g) # 根据KEY取配置文件里面的值 except configparser.NoOptionError as e: if hasattr(Context,g): #Context是否有属性g v=getattr(Context,g) else: print('找不到参数化的值') raise e # print(v) data = re.sub(self.p, v, data, count=1) # 替换 return data
def webservice(self,url,data,method): #要访问的Webservice地址 url=config.get('api_web','pre_url')+url #url拼接 #创建Webservice Client对象 client = Client(url) # print(client)#可以打印出Client对象所有的方法 # data={"client_ip":"192.168.0.105","tmpl_id":"1","mobile":"18762725696"}#用字典的方式传值 try: resp = eval("client.service.{0}({1})".format(method, data)) msg = resp.retInfo # print("返回码", resp.retCode) # print("返回信息", resp.retInfo) except suds.WebFault as e: # print(e.fault.faultstring) msg = e.fault.faultstring return msg