webapi.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import requests, json
  2. from hytest.common import *
  3. from cfg import cfg
  4. from requests.packages import urllib3
  5. #存放公用方法
  6. # 存储 全局共享 数据
  7. GSTORE = {}
  8. class APIMgr():
  9. #打印https请求与消息
  10. def printRequest(self,req):
  11. if req.body==None:
  12. msgBody=''
  13. else:
  14. msgBody=req.body
  15. self.ui.outputWindow.append(
  16. '{}\n{}\n{}\n\n{}'.format(
  17. '\n\n-------发送请求--------',
  18. req.method+''+req.url,
  19. '\n'.join('{}:{}'.format(k,v) for k,v in req.headers.items()),
  20. msgBody,
  21. ))
  22. # 打印http相应消息的函数
  23. def printResponse(self, response):
  24. print('\n\n----- https response begin -----')
  25. print(response.status_code)
  26. # print(response.headers)
  27. for k, v in response.headers.items():
  28. print(f'{k}:{v}')
  29. print(response.content.decode('utf8'))
  30. print('----- https response end-----\n\n')
  31. headers = {
  32. "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"
  33. }
  34. #headers设置为全局变量
  35. GSTORE['headers'] = headers
  36. #登录接口
  37. def mgr_login(self, phone='15225181827', password='123456',useproxies=False):
  38. headers=GSTORE['headers']
  39. self.s = requests.Session()
  40. if useproxies:
  41. self.s.proxies.update({'http':'127.0.0.1:8888'})
  42. response = self.s.post(f"{cfg.target_host}/phone/login",headers=headers,data=
  43. {
  44. 'reqType': 'phoneLogin',
  45. 'isAutoLogin': 'false',
  46. 'phone':phone,
  47. 'password':password
  48. })
  49. self.printResponse(response)
  50. # 把response对象返回出去
  51. return response
  52. #获取推送记录接口
  53. def push_list(self):
  54. headers=GSTORE['headers']
  55. response = self.s.post(f"{cfg.target_host}/jyapi/jybx/subscribe/fType/list",headers=headers,json=
  56. {"pageNum": 1, "pageSize": 50, "format": "table", "area": "", "selectTime": "all", "city": "", "buyerClass": "",
  57. "subtype": "", "industry": "", "keyWords": "", "fileExists": "", "price": "", "source": "", "exportNum": "",
  58. "vt": ""})
  59. return response
  60. apimgr = APIMgr()