webapi.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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='18211989987', 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 search(self, keywords="建筑", publishtime="fiveyear", selectType="content"):
  54. #使用全局变量
  55. headers = GSTORE['headers']
  56. #保存session
  57. session = self.s
  58. response = session.post(f"{cfg.target_host}/jylab/supsearch/index.html", headers=headers, data={
  59. 'keywords': keywords,
  60. 'publishtime': publishtime,
  61. 'selectType': selectType
  62. })
  63. self.printResponse(response)
  64. return response
  65. # 企业搜索
  66. def enterpriseSearch(self, match="北京剑鱼信息技术有限公司河南分公司", matchType="A", pageSize="10", pageNum="0"):
  67. # 使用全局变量
  68. headers = GSTORE['headers']
  69. # 保存session
  70. session = self.s
  71. response = session.post(f"{cfg.target_host}/publicapply/enterpriseSearch/doQuery", headers=headers, data={
  72. 'match': match,
  73. 'matchType': matchType,
  74. 'pageSize': pageSize,
  75. 'pageNum': pageNum
  76. })
  77. self.printResponse(response)
  78. return response
  79. # 供应搜索
  80. def supplySearch(self, keywords="PH计", searchType="title", province="", city="", time="", status="0",
  81. pageSize=50, pageIndex=1):
  82. headers = GSTORE['headers']
  83. headers['Content-Type'] = 'application/json' # 添加Content-Type头部
  84. url = f"{cfg.target_host}/jyinfo/supplySearch"
  85. data = {
  86. "keywords": keywords,
  87. "searchType": searchType,
  88. "province": province,
  89. "city": city,
  90. "time": time,
  91. "status": status,
  92. "pageSize": pageSize,
  93. "pageIndex": pageIndex
  94. }
  95. session=self.s
  96. response = session.post(url=url, headers=headers, data=json.dumps(data))
  97. self.printResponse(response)
  98. return response
  99. #采购单位搜索
  100. def buyersousuo(self, buyerName, province=[], city=[], buyerClass=[], isCheckFollow=True, isCheckReceive=True,
  101. isContact=0, pageSize=10,pageNum=1):
  102. url = f"{cfg.target_host}/jyapi/jybx/buyer/eType/buyerList"
  103. data = {
  104. "buyerName": buyerName,
  105. "province": province,
  106. "city": city,
  107. "buyerClass": buyerClass,
  108. "isCheckFollow": isCheckFollow,
  109. "isCheckReceive": isCheckReceive,
  110. "isContact": isContact,
  111. "pageSize": pageSize,
  112. "pageNum":pageNum
  113. }
  114. response = requests.post(url=url, data=json.dumps(data), headers=self.headers)
  115. self.printResponse(response)
  116. return response
  117. apimgr = APIMgr()