import requests, json from hytest.common import * from cfg import cfg from requests.packages import urllib3 #存放公用方法 # 存储 全局共享 数据 GSTORE = {} class APIMgr(): #打印https请求与消息 def printRequest(self,req): if req.body==None: msgBody='' else: msgBody=req.body self.ui.outputWindow.append( '{}\n{}\n{}\n\n{}'.format( '\n\n-------发送请求--------', req.method+''+req.url, '\n'.join('{}:{}'.format(k,v) for k,v in req.headers.items()), msgBody, )) # 打印http相应消息的函数 def printResponse(self, response): print('\n\n----- https response begin -----') print(response.status_code) # print(response.headers) for k, v in response.headers.items(): print(f'{k}:{v}') print(response.content.decode('utf8')) print('----- https response end-----\n\n') headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67" } #headers设置为全局变量 GSTORE['headers'] = headers #登录接口 def mgr_login(self, phone='15225181827', password='123456',useproxies=False): headers=GSTORE['headers'] self.s = requests.Session() if useproxies: self.s.proxies.update({'http':'127.0.0.1:8888'}) response = self.s.post(f"{cfg.target_host}/phone/login",headers=headers,data= { 'reqType': 'phoneLogin', 'isAutoLogin': 'false', 'phone':phone, 'password':password }) self.printResponse(response) # 把response对象返回出去 return response def search(self, keywords="建筑", publishtime="fiveyear", selectType="content"): headers = GSTORE['headers'] session = self.s response = session.post(f"{cfg.target_host}/jylab/supsearch/index.html", headers=headers, data={ 'keywords': keywords, 'publishtime': publishtime, 'selectType': selectType }) self.printResponse(response) return response #函数 apimgr = APIMgr()