Exemple #1
0
class OperationJson:
    def __init__(self):
        self.excel = OperationExcel()
        self.pm = PublicMethod()

    def getReadJson(self, ensure_ascii=False):
        '''读取到json文件中的内容'''
        with open(self.pm.data_dir('data', 'jsonData.json'),
                  encoding='utf-8') as f:
            data = json.load(f)
            return data

    def getRequestsData(self, row, ensure_ascii=False):
        '''获取请求参数'''
        return self.getReadJson()[self.excel.get_Params(row=row)]
Exemple #2
0
class WebMethon(PublicMethod):
    def __init__(self):
        self.config = Config()
        self.excel = OperationExcel()
        self.public = PublicMethod()
        self.json = OperationJson()

#=================封装请求头,s=0,未登录的请求头。s=1,已登陆的请求头==============================

    def getHeadersInfo(
        self,
        row,
    ):
        '''已登录的请求头'''
        headers = {
            'Host': self.config.getHost(),
            #'Content-Type': self.excel.get_ParamsType(row=row),
            'Accept-Encoding': 'gzip, deflate',
            'Cookie':
            'Hm_lvt_950d894867df0a55fbb3239fba8837ea=1588758701,1588774347,1588833146,1588856383',
            'Accept': '*/*',
            'User-Agent': 'DDOU/1.0.3 (iPhone; iOS 13.3.1; Scale/3.00)',
            'Authorization': self.public.getFile('data', 'login', 'token'),
            #'Referer': self.config.getUrl()[0]+self.excel.get_Referer(row=row)
        }
        return headers

    def post(self, s, row, **kwargs):
        '''
		请求参数是json格式的post,这里读取的是excel+json文件中的数据
		s=1:json格式的参数
		s=2:data格式的参数
		'''
        if s == 1:
            try:
                r = requests.post(url=self.config.getUrl()[0] +
                                  self.excel.get_Url(row=row),
                                  json=self.json.getRequestsData(row=row),
                                  headers=self.getHeadersInfo(row=row),
                                  timeout=5)
                return r
            except Exception as e:
                raise RuntimeError('接口请求法生未知的错误')
        elif s == 2:
            '''请求方法是data格式的post,这里读取得是excel文件中的数据'''
            try:
                r = requests.post(url=self.excel.get_Url(row=row),
                                  data=self.excel.get_Params(row=row),
                                  headers=self.getHeadersInfo(row=row),
                                  timeout=5)
                return r
            except Exception as e:
                raise RuntimeError('接口请求法生未知的错误')
        else:
            print('Eorr:请检查请求参数!')

    def get(self, url, params=None):
        '''对get请求进行二次封装'''
        r = requests.get(url=url,
                         params=params,
                         headers=self.getHeadersInfo(),
                         timeout=5)
        return r