本文整理汇总了Python中urllib3.HTTPResponse方法的典型用法代码示例。如果您正苦于以下问题:Python urllib3.HTTPResponse方法的具体用法?Python urllib3.HTTPResponse怎么用?Python urllib3.HTTPResponse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib3
的用法示例。
在下文中一共展示了urllib3.HTTPResponse方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_urlopen_mock
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import HTTPResponse [as 别名]
def get_urlopen_mock(body=DEFAULT_BODY_CONTENT, headers=None, status=200):
mockHttpResponse = Mock(name='httplib.HTTPResponse')
headers = urllib3.response.HTTPHeaderDict(headers)
if not hasattr(body, 'read'):
body = BytesIO(body)
else:
body.seek(0)
urllib3_response = urllib3.HTTPResponse(body,
headers,
status,
preload_content=False,
original_response=mockHttpResponse)
return MagicMock(return_value=urllib3_response)
示例2: _provide_ssl_auth_required
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import HTTPResponse [as 别名]
def _provide_ssl_auth_required(self):
"""
Provide ssl auth response
:return: urllib3.HTTPResponse
"""
response = HTTPResponse()
response.status = self.DEFAULT_SSL_CERT_REQUIRED_STATUSES
response.__setattr__('_body', ' ')
return response
示例3: json
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import HTTPResponse [as 别名]
def json(self) -> Dict[str, Any]:
"""Convert the response data to JSON.
If the response data is not valid JSON, an error will be raised.
Returns:
The JSON data loaded into a Python dictionary.
"""
# This should generally be the case, since the primary source of Response
# instances are the HTTP methods on Pods (e.g. http_proxy_get), where
# `_preload_content=False`. Should that be set to True, it will not return
# the HTTPResponse but will instead return the response data formatted as
# the type specified in the `response_type` param. By default, kubetest sets
# the _preload_content field to True, so this should generally not be hit.
if isinstance(self.data, urllib3.HTTPResponse):
return json.loads(self.data.data)
# The response data comes back as a string. This could be a JSON string,
# or something else (text body, error string, etc). Since we've preloaded
# the content as a string (see comment above), we can not simply load the
# string as JSON as the preloading essentially serializes the Python dict
# out to a string, so various values will not parse into JSON (None vs null,
# " vs ', etc). To remedy this, we attempt to load the response as an ast
# literal - if it fails for any reason, fall back to trying to load JSON.
# At the very least the JSON loading will fail with a familiar error which
# one would expect from a .json() function.
try:
data = ast.literal_eval(self.data)
except Exception as e:
log.debug(f'failed literal eval of data {self.data} ({e})')
data = json.loads(self.data)
return data
示例4: __init__
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import HTTPResponse [as 别名]
def __init__(self, response: HTTPResponse, serialization_mode: FluxSerializationMode,
data_frame_index: List[str] = None) -> None:
self._response = response
self.tables = []
self._serialization_mode = serialization_mode
self._data_frame_index = data_frame_index
self._data_frame_values = []
pass
示例5: _parse_to_tables
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import HTTPResponse [as 别名]
def _parse_to_tables(data: str):
fp = BytesIO(str.encode(data))
_parser = FluxCsvParser(response=HTTPResponse(fp, preload_content=False),
serialization_mode=FluxSerializationMode.tables)
list(_parser.generator())
tables = _parser.tables
return tables
示例6: __init__
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import HTTPResponse [as 别名]
def __init__(self, resp):
# arg: A urllib3.HTTPResponse object
self.urllib3_response = resp
self.status = resp.status
self.version = resp.version
self.reason = resp.reason
self.strict = resp.strict
self.is_closed = False
示例7: closed
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import HTTPResponse [as 别名]
def closed(self):
return self.is_closed
# ---------------------------------
# Backwards compat for HTTPResponse
# ---------------------------------
示例8: test_run_next_exception
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import HTTPResponse [as 别名]
def test_run_next_exception(self, mock_get_kube_client, mock_kubernetes_job_watcher):
# When a quota is exceeded this is the ApiException we get
response = HTTPResponse(
body='{"kind": "Status", "apiVersion": "v1", "metadata": {}, "status": "Failure", '
'"message": "pods \\"podname\\" is forbidden: exceeded quota: compute-resources, '
'requested: limits.memory=4Gi, used: limits.memory=6508Mi, limited: limits.memory=10Gi", '
'"reason": "Forbidden", "details": {"name": "podname", "kind": "pods"}, "code": 403}')
response.status = 403
response.reason = "Forbidden"
# A mock kube_client that throws errors when making a pod
mock_kube_client = mock.patch('kubernetes.client.CoreV1Api', autospec=True)
mock_kube_client.create_namespaced_pod = mock.MagicMock(
side_effect=ApiException(http_resp=response))
mock_get_kube_client.return_value = mock_kube_client
mock_api_client = mock.MagicMock()
mock_api_client.sanitize_for_serialization.return_value = {}
mock_kube_client.api_client = mock_api_client
kubernetes_executor = KubernetesExecutor()
kubernetes_executor.start()
# Execute a task while the Api Throws errors
try_number = 1
kubernetes_executor.execute_async(key=('dag', 'task', datetime.utcnow(), try_number),
queue=None,
command=['airflow', 'tasks', 'run', 'true', 'some_parameter'],
executor_config={})
kubernetes_executor.sync()
kubernetes_executor.sync()
assert mock_kube_client.create_namespaced_pod.called
self.assertFalse(kubernetes_executor.task_queue.empty())
# Disable the ApiException
mock_kube_client.create_namespaced_pod.side_effect = None
# Execute the task without errors should empty the queue
kubernetes_executor.sync()
assert mock_kube_client.create_namespaced_pod.called
self.assertTrue(kubernetes_executor.task_queue.empty())
示例9: request
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import HTTPResponse [as 别名]
def request(self, url):
"""
Client request SSL
:param str url: request uri
:return: urllib3.HTTPResponse
"""
if self._HTTP_DBG_LEVEL <= self.__debug.level:
self.__debug.debug_request(self._headers, url, self.__cfg.method)
try:
disable_warnings(InsecureRequestWarning)
if self.__cfg.DEFAULT_SCAN == self.__cfg.scan: # directories requests
response = self.__pool.request(self.__cfg.method,
helper.parse_url(url).path,
headers=self._headers,
retries=self.__cfg.retries,
assert_same_host=False,
redirect=False)
self.cookies_middleware(is_accept=self.__cfg.accept_cookies, response=response)
else: # subdomains
response = PoolManager().request(self.__cfg.method, url,
headers=self._headers,
retries=self.__cfg.retries,
assert_same_host=False,
redirect=False)
return response
except MaxRetryError:
if self.__cfg.DEFAULT_SCAN == self.__cfg.scan:
self.__tpl.warning(key='max_retry_error', url=helper.parse_url(url).path)
except HostChangedError as error:
self.__tpl.warning(key='host_changed_error', details=error)
pass
except ReadTimeoutError:
self.__tpl.warning(key='read_timeout_error', url=url)
pass
except ConnectTimeoutError:
self.__tpl.warning(key='connection_timeout_error', url=url)
pass
except SSLError:
if self.__cfg.DEFAULT_SCAN != self.__cfg.scan:
return self._provide_ssl_auth_required()