wxapi.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import json
  2. import requests
  3. from hytest.common import *
  4. from cfg import cfg
  5. from bs4 import BeautifulSoup
  6. from requests.packages import urllib3
  7. #存放公用方法
  8. # 存储 全局共享 数据
  9. GSTORE = {}
  10. class APIMgr():
  11. #打印https请求与消息
  12. def __init__(self):
  13. self.ui = None
  14. self.token = None
  15. def printRequest(self,req):
  16. if req.body==None:
  17. msgBody=''
  18. else:
  19. msgBody=req.body
  20. self.ui.outputWindow.append(
  21. '{}\n{}\n{}\n\n{}'.format(
  22. '\n\n-------发送请求--------',
  23. req.method+''+req.url,
  24. '\n'.join('{}:{}'.format(k,v) for k,v in req.headers.items()),
  25. msgBody,
  26. ))
  27. # 打印http相应消息的函数
  28. def printResponse(self, response):
  29. print('\n\n----- https response begin -----')
  30. print(response.status_code)
  31. # print(response.headers)
  32. for k, v in response.headers.items():
  33. print(f'{k}:{v}')
  34. print(response.content.decode('utf8'))
  35. print('----- https response end-----\n\n')
  36. headers = {
  37. 'Accept-Charset':'utf-8',
  38. 'Accept':'*/*',
  39. 'Accept-Encoding':'gzip, deflate, br',
  40. 'User-Agent':'Apifox/1.0.0 (https://apifox.com)',
  41. 'Content-Type':'application/json',
  42. 'MiniprogramCode':'wy_zbxm'
  43. }
  44. #headers设置为全局变量
  45. GSTORE['headers'] = headers
  46. # session对象设置为全局变量
  47. s = requests.Session()
  48. GSTORE['s'] = s
  49. # 小程序登录接口
  50. def mgr_login_wxapi(self, useproxies=False):
  51. headers=GSTORE['headers']
  52. s = GSTORE['s']
  53. if useproxies:
  54. self.s.proxies.update({'http':'127.0.0.1:8888'})
  55. response = self.s.post(f"{cfg.target_host_wxapi}/debrisproduct/free/autologin",headers=headers,json=
  56. {
  57. 'phone':'15037870765',
  58. 'token':'8817684142977367381'
  59. })
  60. self.printResponse(response)
  61. # 把response对象返回出去
  62. return response
  63. """退出登录小程序"""
  64. def mgr_logout_wxapi(self):
  65. url = f"{cfg.target_host_wxapi}/debrisproduct/logout"
  66. headers = GSTORE['headers']
  67. s = GSTORE['s']
  68. s.post(url=url, headers=headers)
  69. """我的用户信息"""
  70. def myuser_information(self):
  71. hearders = GSTORE['headers']
  72. s = GSTORE['s']
  73. response = s.post(f"{cfg.target_host_wxapi}/debrisproduct/myinfo", headers=hearders)
  74. return response
  75. apimgr = APIMgr()