http_response.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. # coding=utf-8
  18. import os
  19. import logging
  20. from aliyunsdkcore.vendored.requests import Request, Session
  21. from aliyunsdkcore.vendored.requests.packages import urllib3
  22. from aliyunsdkcore.http.http_request import HttpRequest
  23. from aliyunsdkcore.http import protocol_type as PT
  24. from aliyunsdkcore.vendored.requests import status_codes
  25. logger = logging.getLogger(__name__)
  26. logger.setLevel(logging.DEBUG)
  27. ch = logging.StreamHandler()
  28. logger.addHandler(ch)
  29. DEFAULT_CONNECT_TIMEOUT = 5
  30. class HttpResponse(HttpRequest):
  31. def __init__(
  32. self,
  33. host="",
  34. url="/",
  35. method="GET",
  36. headers={},
  37. protocol=PT.HTTP,
  38. content=None,
  39. port=None,
  40. key_file=None,
  41. cert_file=None,
  42. read_timeout=None,
  43. connect_timeout=None,
  44. verify=None):
  45. HttpRequest.__init__(
  46. self,
  47. host=host,
  48. url=url,
  49. method=method,
  50. headers=headers)
  51. self.__ssl_enable = False
  52. if protocol is PT.HTTPS:
  53. self.__ssl_enable = True
  54. self.__key_file = key_file
  55. self.__cert_file = cert_file
  56. self.__port = port
  57. self.__connection = None
  58. self.__read_timeout = read_timeout
  59. self.__connect_timeout = connect_timeout
  60. self.__verify = verify
  61. self.set_body(content)
  62. def set_ssl_enable(self, enable):
  63. self.__ssl_enable = enable
  64. def get_ssl_enabled(self):
  65. return self.__ssl_enable
  66. @staticmethod
  67. def prepare_http_debug(request, symbol):
  68. base = ''
  69. for key, value in request.headers.items():
  70. base += '\n%s %s : %s' % (symbol, key, value)
  71. return base
  72. def do_http_debug(self, request, response):
  73. # logger the request
  74. request_base = '\n> %s %s HTTP/1.1' % (self.get_method().upper(), self.get_url())
  75. request_base += '\n> Host : %s' % self.get_host()
  76. logger.debug(request_base + self.prepare_http_debug(request, '>'))
  77. # logger the response
  78. response_base = '\n< HTTP/1.1 %s %s' % (
  79. response.status_code, status_codes._codes.get(response.status_code)[0].upper())
  80. logger.debug(response_base + self.prepare_http_debug(response, '<'))
  81. def get_verify_value(self):
  82. if self.__verify is not None:
  83. return self.__verify
  84. return os.environ.get('ALIBABA_CLOUD_CA_BUNDLE', True)
  85. def get_response_object(self):
  86. with Session() as s:
  87. current_protocol = 'https://' if self.get_ssl_enabled() else 'http://'
  88. url = current_protocol + self.get_host() + self.get_url()
  89. if self.__port != 80:
  90. url = current_protocol + self.get_host() + ":" + str(self.__port) + self.get_url()
  91. req = Request(method=self.get_method(), url=url,
  92. data=self.get_body(),
  93. headers=self.get_headers(),
  94. )
  95. prepped = s.prepare_request(req)
  96. proxy_https = os.environ.get('HTTPS_PROXY') or os.environ.get(
  97. 'https_proxy')
  98. proxy_http = os.environ.get(
  99. 'HTTP_PROXY') or os.environ.get('http_proxy')
  100. proxies = {
  101. "http": proxy_http,
  102. "https": proxy_https,
  103. }
  104. response = s.send(prepped, proxies=proxies,
  105. timeout=(self.__connect_timeout, self.__read_timeout),
  106. allow_redirects=False, verify=self.get_verify_value(), cert=None)
  107. http_debug = os.environ.get('DEBUG')
  108. if http_debug is not None and http_debug.lower() == 'sdk':
  109. # http debug information
  110. self.do_http_debug(prepped, response)
  111. return response.status_code, response.headers, response.content