webapi.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. # session对象设置为全局变量
  37. s = requests.Session()
  38. GSTORE['s'] = s
  39. #登录接口
  40. def mgr_login(self, phone='18211989987', password='123456',useproxies=False):
  41. headers=GSTORE['headers']
  42. s = GSTORE['s']
  43. if useproxies:
  44. self.s.proxies.update({'http':'127.0.0.1:8888'})
  45. response = self.s.post(f"{cfg.target_host}/phone/login",headers=headers,data=
  46. {
  47. 'reqType': 'phoneLogin',
  48. 'isAutoLogin': 'false',
  49. 'phone':phone,
  50. 'password':password
  51. })
  52. self.printResponse(response)
  53. # 把response对象返回出去
  54. return response
  55. def mgr_logout(self):
  56. url = f"{cfg.target_host}/front/signOut"
  57. headers = GSTORE['headers']
  58. s = GSTORE['s']
  59. s.post(url=url, headers=headers)
  60. # self.printResponse(res)
  61. # return res
  62. def search(self, keywords="建筑", publishtime="fiveyear", selectType="content"):
  63. #使用全局变量
  64. headers = GSTORE['headers']
  65. #保存session
  66. session = self.s
  67. response = session.post(f"{cfg.target_host}/jylab/supsearch/index.html", headers=headers, data={
  68. 'keywords': keywords,
  69. 'publishtime': publishtime,
  70. 'selectType': selectType
  71. })
  72. self.printResponse(response)
  73. return response
  74. # 企业搜索
  75. def enterpriseSearch(self, match="北京剑鱼信息技术有限公司河南分公司", matchType="A", pageSize="10", pageNum="0"):
  76. # 使用全局变量
  77. headers = GSTORE['headers']
  78. # 保存session
  79. session = self.s
  80. response = session.post(f"{cfg.target_host}/publicapply/enterpriseSearch/doQuery", headers=headers, data={
  81. 'match': match,
  82. 'matchType': matchType,
  83. 'pageSize': pageSize,
  84. 'pageNum': pageNum
  85. })
  86. self.printResponse(response)
  87. return response
  88. # 供应搜索
  89. def supplySearch(self, keywords="PH计", searchType="title", province="", city="", time="", status="0",
  90. pageSize=50, pageIndex=1):
  91. headers = GSTORE['headers']
  92. headers['Content-Type'] = 'application/json' # 添加Content-Type头部
  93. url = f"{cfg.target_host}/jyinfo/supplySearch"
  94. data = {
  95. "keywords": keywords,
  96. "searchType": searchType,
  97. "province": province,
  98. "city": city,
  99. "time": time,
  100. "status": status,
  101. "pageSize": pageSize,
  102. "pageIndex": pageIndex
  103. }
  104. session=self.s
  105. response = session.post(url=url, headers=headers, data=json.dumps(data))
  106. self.printResponse(response)
  107. return response
  108. #采购单位搜索
  109. def buyersousuo(self, buyerName, province=[], city=[], buyerClass=[], isCheckFollow=True, isCheckReceive=True,
  110. isContact=0, pageSize=10,pageNum=1):
  111. url = f"{cfg.target_host}/jyapi/jybx/buyer/eType/buyerList"
  112. data = {
  113. "buyerName": buyerName,
  114. "province": province,
  115. "city": city,
  116. "buyerClass": buyerClass,
  117. "isCheckFollow": isCheckFollow,
  118. "isCheckReceive": isCheckReceive,
  119. "isContact": isContact,
  120. "pageSize": pageSize,
  121. "pageNum":pageNum
  122. }
  123. response = requests.post(url=url, json=data, headers=self.headers)
  124. self.printResponse(response)
  125. return response
  126. #获取推送记录接口
  127. def push_list(self):
  128. headers=GSTORE['headers']
  129. s = GSTORE['s']
  130. response = self.s.post(f"{cfg.target_host}/jyapi/jybx/subscribe/fType/list",headers=headers,json=
  131. {"pageNum": 1, "pageSize": 50, "format": "table", "area": "", "selectTime": "all", "city": "", "buyerClass": "",
  132. "subtype": "", "industry": "", "keyWords": "", "fileExists": "", "price": "", "source": "", "exportNum": "",
  133. "vt": ""})
  134. return response
  135. #不登录招标搜索
  136. def notloggedin_search(self, keyword='科技', publishtime='thisyear', selectType='content,title'):
  137. headers = GSTORE['headers']
  138. params={"keywords": keyword , "publishtime": publishtime, "timeslot": "", "area": "", "subtype": "",
  139. "minprice": "", "maxprice": "", "industry": "", "buyerclass": "", "buyertel": "", "winnertel": "",
  140. "selectType": selectType, "notkey": "", "fileExists": "0", "city": "", "searchGroup": "0",
  141. "searchMode": "0", "wordsMode": "0", "additionalWords": ""}
  142. response = requests.post(f"{cfg.target_host}/jylab/supsearch/index.html", headers=headers, params=params)
  143. response.raise_for_status() # 如果请求失败,会抛出异常
  144. return response
  145. #不登录采购单位搜索
  146. def notloggedin_buysearch(self,keyword):
  147. headers = GSTORE['headers']
  148. s = GSTORE['s']
  149. data = {"buyerName":keyword,"province":[],"city":[],"buyerClass":[],"isCheckFollow":False,"isCheckReceive":False,"isContact":0,"pageSize":10,"pageNum":1}
  150. response =s.post(f"{cfg.target_host}/jyapi/jybx/buyer/eType/buyerList", headers=headers, json=data)
  151. # response.raise_for_status() # 如果请求失败,会抛出异常
  152. return response
  153. #消息中心列表
  154. def get_messagelist(self):
  155. headers = GSTORE['headers']
  156. s = GSTORE['s']
  157. response=s.post(f"{cfg.target_host}/jymessageCenter/messageList",headers=headers,params=
  158. {"needFilter":"1","msgType":"0","isRead":"-1","offset":"1","pageSize":"5"})
  159. return response
  160. apimgr = APIMgr()