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.

78 satır
3.7KB

  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. headers = {'content-type': "application/json"}
  62. req = HttpRequests()
  63. response = req.send_request('POST', 'http://127.0.0.1:8000/items/222', headers=headers, timeout=3)
  64. print(response.__dict__)
  65. print(response.status_code)
  66. print(type(response.content), isinstance(response.content, bytes))
  67. print(response.content.decode('utf-8'))
  68. req.close_session()