You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
4.5KB

  1. # -*- coding: utf-8 -*-
  2. import time
  3. from requests.adapters import HTTPAdapter
  4. import json
  5. import requests
  6. class HttpRequests:
  7. __slots__ = ("session")
  8. def __init__(self):
  9. self.session = requests.Session()
  10. self.session.mount('http://', HTTPAdapter(max_retries=3))
  11. self.session.mount('https://', HTTPAdapter(max_retries=3))
  12. def send_request(self, method, url, data=None, params_type='JSON', **kwargs):
  13. method = method.upper()
  14. params_type = params_type.upper()
  15. if isinstance(data, str):
  16. try:
  17. data = json.loads(data)
  18. except Exception:
  19. data = eval(data)
  20. if 'GET' == method:
  21. response = self.session.request(method=method, url=url, params=data, **kwargs)
  22. elif 'POST' == method:
  23. if params_type == 'FORM': # 发送表单数据,使用data参数传递
  24. response = self.session.request(method=method, url=url, data=data, **kwargs)
  25. elif params_type == 'JSON': # 如果接口支持application/json类型,则使用json参数传递
  26. response = self.session.request(method=method, url=url, json=data, **kwargs)
  27. else: # 如果接口需要传递其他类型的数据比如 上传文件,调用下面的请求方法
  28. response = self.session.request(method=method, url=url, **kwargs)
  29. elif 'PUT' == method:
  30. if params_type == 'FORM': # 发送表单数据,使用data参数传递
  31. response = self.session.request(method=method, url=url, data=data, **kwargs)
  32. elif params_type == 'JSON': # 如果接口支持application/json类型,则使用json参数传递
  33. response = self.session.request(method=method, url=url, json=data, **kwargs)
  34. else: # 如果接口需要传递其他类型的数据比如 上传文件,调用下面的请求方法
  35. response = self.session.request(method=method, url=url, **kwargs)
  36. elif 'DELETE' == method:
  37. if params_type == 'FORM': # 发送表单数据,使用data参数传递
  38. response = self.session.request(method=method, url=url, data=data, **kwargs)
  39. elif params_type == 'JSON': # 如果接口支持application/json类型,则使用json参数传递
  40. response = self.session.request(method=method, url=url, json=data, **kwargs)
  41. else: # 如果接口需要传递其他类型的数据比如 上传文件,调用下面的请求方法
  42. response = self.session.request(method=method, url=url, **kwargs)
  43. # 如果请求方式非 get 和post 会报错,当然你也可以继续添加其他的请求方法
  44. else:
  45. raise ValueError('request method "{}" error ! please check'.format(method))
  46. return response
  47. def __call__(self, method, url, data=None, params_type='JSON', **kwargs):
  48. return self.send_request(method, url,
  49. data=data,
  50. params_type=params_type,
  51. **kwargs)
  52. def close_session(self):
  53. self.session.close()
  54. try:
  55. del self.session.cookies['JSESSIONID']
  56. except:
  57. pass
  58. def __del__(self):
  59. self.close_session()
  60. if __name__ == '__main__':
  61. start_time = time.time()
  62. headers = {'content-type': "application/json"}
  63. req = HttpRequests()
  64. response1 = req.send_request('GET', "http://localhost:19610/api/v1/getChannelList?offset=0&row=1", headers=headers, timeout=3)
  65. result = response1.content.decode('utf-8')
  66. print(result)
  67. result = json.loads(result)
  68. result = result["ChannelList"]
  69. print(result[0]["enable"], type(result[0]["enable"]))
  70. result[0]["enable"] = "true"
  71. result[0]["srcURL"] = '"%s"' % result[0]["srcURL"]
  72. param = 'indexCode=%s&name=%s&srcURL=%s&connectType=%s&timeout=5&mediaType=%s&dstURL=%s&dstFormat=%s&enable=%s' %\
  73. (result[0]["indexcode"], result[0]["name"], result[0]["srcURL"], result[0]["connectType"],
  74. result[0]["mediaType"], result[0]["dstURL"], result[0]["dstFormat"],result[0]["enable"])
  75. url = 'http://localhost:19610/api/v1/updateChannel?%s' % param
  76. response = req.send_request('GET', url, headers=headers, timeout=10)
  77. print(response.__dict__)
  78. print(response.status_code)
  79. print(type(response.content), isinstance(response.content, bytes))
  80. print(response.content.decode('utf-8'), type(response.content.decode('utf-8')))
  81. req.close_session()
  82. print(time.time() - start_time)