def __init__(self): # host = "test.lemonban.com" # port = 3306 # user = "******" # password = "******" host = config.get('mysql', 'host') port = config.get('mysql', 'port') user = config.get('mysql', 'user') password = config.get('mysql', 'password') self.mysql = pymysql.connect(host=host, port=int(port), user=user, password=password) #数据库连接 self.cursor = self.mysql.cursor( pymysql.cursors.DictCursor) # 新建游标查询,括号内表示创建一个字典类型游标,返回的数据为字典
def request(self, method, url, data=None, json=None): """ method:请求方法 url:请求地址 data/json:请求参数 """ if type(data) == str: data = eval(data) print("请求data:", data) # 拼接请求的url url = config.get("api", "pre_url") + url #读取配置文件的前半段 logger.debug("请求url:{0}".format(url)) logger.debug("请求data:{0}".format(data)) if method.lower() == "get": try: resp = self.session.request("get", url=url, params=data) except Exception as e: print("get方法有误:{}".format(e)) elif method.lower() == "post": try: if json: resp = self.session.request("post", url=url, json=json) else: resp = self.session.request("post", url=url, data=data) except Exception as e: print("post方法有误:{}".format(e)) else: resp = None logger.error('UN-support method') logger.debug("请求response:", resp.text) return resp
def request(self, method, url, data=None, json=None, cookie=None): """ method:请求方法 url:请求地址 data/json:请求参数 """ if type(data) == str: data = eval(data) print("请求data:", data) #拼接请求的url url = config.get("api", "pre_url") + url #读取配置文件的前半段 logger.debug("请求url:{0}".format(url)) logger.debug("请求data:{0}".format(data)) if method.lower() == 'get': #判断大小写,发送get请求 try: resp = requests.get(url, params=data, cookies=cookie) except Exception as e: print("get请求出错:{}".format(e)) elif method.lower() == "post": try: if json: resp = requests.post(url, json=json, cookies=cookie) else: resp = requests.post(url, data=data, cookies=cookie) except Exception as e: print("post请求出错:{}".format(e)) else: resp = None print('UN-support method') print("请求response:", resp.text) return resp
def get_logger(name): logger = logging.getLogger(name) #建立一个日志收集器 logger.setLevel("DEBUG") #设定日志收集级别 fmt = "%(name)s - %(levelname)s - %(asctime)s - %(message)s - [%(filename)s:%(lineno)d]" formatter = logging.Formatter(fmt=fmt) #设定日志输出格式 console_handler = logging.StreamHandler() #指定输出到控制台 #吧日志级别放到配置文件里面去配置-- 优化 gather = config.get('log', 'gather_log') console_handler.setLevel(gather) #指定输出级别 console_handler.setFormatter(formatter) file_handler = logging.FileHandler(contants.log_dir + "/case.log") # 吧日志级别放到配置文件里面去配置 output = config.get('log', 'output_log') file_handler.setLevel(output) file_handler.setFormatter(formatter) logger.addHandler(console_handler) logger.addHandler(file_handler) return logger
def replace(data): p = "#(.*?)#" #正则表达式 while re.search(p,data): #从任意位置开始找,找到第一个就返回match object,如果没有就返回none print(data) m = re.search(p,data) #查找匹配合适正则表达式的数据 print("m是:{}".format(m)) g = m.group(1) #拿到参数化的key,只返回指定组的内容 #g = m.group(0) #放回表达式和组里面的内容,#()#表示组 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 = re.sub(p,v,data,count=1) #查找切替换data中的数据。count为查找替换的次数 return data
import re from API_1.common.config import config #解析正则表达式,-->查找 data = '{"mobilephone":"#normal_user#","pwd":"#normal_pwd#"}' #原本字符,元字符 p = "#(.*?)#" #正则表达式 # ms = re.findall(p,data) #查找全部,返回列表 m = re.search(p, data) #从任意位置开始找,找到第一个就返回match object,如果没有就返回none print(m) print(m.group(0)) #返回表达式和组里面的内容 print(m.group(1)) #只返回指定组的内容 g = m.group(1) #拿到参数化的key print("g为", g) 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): # m = re.search(p,data) # g = m.group(1) # v = config.get('data',g) # data = re.sub(p,v,data,count=1) # print(data)
import pymysql from API_1.common.config import config # from API_1.common import do_mysql # # mysql = do_mysql.DoMysql() # # while 1: # # new_phone = 15717481995 # # sql = 'select * from future.member where mobilephone = new_phone' # # phone = mysql.fetch_one(sql)[3] host = config.get('mysql', 'host') print(host) port = config.get('mysql', 'port') print(port) user = config.get('mysql', 'user') print(user) password = config.get('mysql', 'password') print(password) mysql = pymysql.connect(host=host, port=int(port), user=user, password=password) #数据库连接