本文整理汇总了Python中urllib3.exceptions.ReadTimeoutError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ReadTimeoutError方法的具体用法?Python exceptions.ReadTimeoutError怎么用?Python exceptions.ReadTimeoutError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib3.exceptions
的用法示例。
在下文中一共展示了exceptions.ReadTimeoutError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch_description
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def fetch_description(url: str) -> list:
"""
:param id: Id of the image whose description will be downloaded
:return: Json
"""
# Sometimes their site isn't responding well, and than an error occurs,
# So we will retry 10 seconds later and repeat until it succeeds
while True:
try:
# Download the description
response_desc = requests.get(url, stream=True, timeout=20)
# Validate the download status is ok
response_desc.raise_for_status()
# Parse the description
parsed_description = response_desc.json()
return parsed_description
except (RequestException, ReadTimeoutError):
time.sleep(5)
示例2: run
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def run(self) -> None:
"""Performs watching"""
kube_client: client.CoreV1Api = get_kube_client()
if not self.worker_uuid:
raise AirflowException(NOT_STARTED_MESSAGE)
while True:
try:
self.resource_version = self._run(kube_client, self.resource_version,
self.worker_uuid, self.kube_config)
except ReadTimeoutError:
self.log.warning("There was a timeout error accessing the Kube API. "
"Retrying request.", exc_info=True)
time.sleep(1)
except Exception:
self.log.exception('Unknown error in KubernetesJobWatcher. Failing')
raise
else:
self.log.warning('Watch died gracefully, starting back up with: '
'last resource_version: %s', self.resource_version)
示例3: read
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def read(self, amt=None):
"""Read at most amt bytes from the stream.
If the amt argument is omitted, read all data.
"""
try:
chunk = self._raw_stream.read(amt)
except URLLib3ReadTimeoutError as e:
# TODO: the url will be None as urllib3 isn't setting it yet
raise ReadTimeoutError(endpoint_url=e.url, error=e)
self._amount_read += len(chunk)
if amt is None or (not chunk and amt > 0):
# If the server sends empty contents or
# we ask to read all of the contents, then we know
# we need to verify the content length.
self._verify_content_length()
return chunk
示例4: push
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def push(destination, docker=None, max_retries=3, sleep_interval=1):
docker_pusher = DockerPusher(destination=destination, docker=docker)
retry = 0
is_done = False
while retry < max_retries and not is_done:
try:
if not docker_pusher.push():
raise PolyaxonBuildException("The docker image could not be pushed.")
else:
is_done = True
except ReadTimeoutError:
retry += 1
time.sleep(sleep_interval)
if not is_done:
raise PolyaxonBuildException(
"The docker image could not be pushed, client timed out."
)
示例5: watch
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def watch(self):
from urllib3.exceptions import ReadTimeoutError
from kubernetes import watch
cur_pods = set(self.get(True))
w = watch.Watch()
while True:
# when some schedulers are not ready, we refresh faster
linger = 10 if self.is_all_ready() else 1
streamer = w.stream(self._client.list_namespaced_pod,
namespace=self._k8s_namespace,
label_selector=self._label_selector,
timeout_seconds=linger)
while True:
try:
event = self._pool.spawn(next, streamer, StopIteration).result()
if event is StopIteration:
raise StopIteration
except (ReadTimeoutError, StopIteration):
new_pods = set(self.get(True))
if new_pods != cur_pods:
cur_pods = new_pods
yield self.get(False)
break
except: # noqa: E722
logger.exception('Unexpected error when watching on kubernetes')
break
obj_dict = event['object'].to_dict()
pod_name, endpoint = self._extract_pod_name_ep(obj_dict)
self._pod_to_ep[pod_name] = endpoint \
if endpoint and self._extract_pod_ready(obj_dict) else None
yield self.get(False)
示例6: download_img
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def download_img(cls, img_url, img_name, dir, max_tries=None):
"""
Download the image from the given url and save it in the given dir,
naming it using img_name with an jpg extension
:param img_name:
:param img_url: Url to download the image through
:param dir: Directory in which to save the image
:return Whether the image was downloaded successfully
"""
# Sometimes their site isn't responding well, and than an error occurs,
# So we will retry a few seconds later and repeat until it succeeds
tries = 0
while max_tries is None or tries <= max_tries:
try:
# print('Attempting to download image {0}'.format(img_name))
response = requests.get(img_url, stream=True, timeout=20)
# Validate the download status is ok
response.raise_for_status()
# Find the format of the image
format = response.headers['Content-Type'].split('/')[1]
# Write the image into a file
image_string = response.raw
img_path = join(dir, '{0}.{1}'.format(img_name, format))
with open(img_path, 'wb') as imageFile:
shutil.copyfileobj(image_string, imageFile)
# Validate the image was downloaded correctly
cls.validate_image(img_path)
# print('Finished Downloading image {0}'.format(img_name))
return True
except (RequestException, ReadTimeoutError, IOError):
tries += 1
time.sleep(5)
return False
示例7: perform_request
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
url = self.url_prefix + url
if params:
url = '%s?%s' % (url, urlencode(params))
full_url = self.host + url
start = time.time()
try:
kw = {}
if timeout:
kw['timeout'] = timeout
# in python2 we need to make sure the url is not unicode. Otherwise
# the body will be decoded into unicode too and that will fail (#133).
if not isinstance(url, str):
url = url.encode('utf-8')
response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw)
duration = time.time() - start
raw_data = response.data.decode('utf-8')
except UrllibSSLError as e:
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
raise SSLError('N/A', str(e), e)
except ReadTimeoutError as e:
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
raise ConnectionTimeout('TIMEOUT', str(e), e)
except Exception as e:
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
raise ConnectionError('N/A', str(e), e)
if not (200 <= response.status < 300) and response.status not in ignore:
self.log_request_fail(method, url, body, duration, response.status)
self._raise_error(response.status, raw_data)
self.log_request_success(method, full_url, url, body, response.status,
raw_data, duration)
return response.status, response.getheaders(), raw_data
示例8: perform_request
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
url = self.url_prefix + url
if params:
url = '%s?%s' % (url, urlencode(params))
full_url = self.host + url
start = time.time()
try:
kw = {}
if timeout:
kw['timeout'] = timeout
# in python2 we need to make sure the url and method are not
# unicode. Otherwise the body will be decoded into unicode too and
# that will fail (#133, #201).
if not isinstance(url, str):
url = url.encode('utf-8')
if not isinstance(method, str):
method = method.encode('utf-8')
response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw)
duration = time.time() - start
raw_data = response.data.decode('utf-8')
except UrllibSSLError as e:
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
raise SSLError('N/A', str(e), e)
except ReadTimeoutError as e:
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
raise ConnectionTimeout('TIMEOUT', str(e), e)
except Exception as e:
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
raise ConnectionError('N/A', str(e), e)
if not (200 <= response.status < 300) and response.status not in ignore:
self.log_request_fail(method, url, body, duration, response.status)
self._raise_error(response.status, raw_data)
self.log_request_success(method, full_url, url, body, response.status,
raw_data, duration)
return response.status, response.getheaders(), raw_data
示例9: build
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def build(
context,
destination,
nocache,
docker=None,
credstore_env=None,
registries=None,
max_retries=3,
sleep_interval=1,
):
"""Build necessary code for a job to run"""
retry = 0
is_done = False
while retry < max_retries and not is_done:
try:
docker_builder = _build(
context=context,
destination=destination,
docker=docker,
nocache=nocache,
credstore_env=credstore_env,
registries=registries,
)
is_done = True
return docker_builder
except ReadTimeoutError:
retry += 1
time.sleep(sleep_interval)
if not is_done:
raise PolyaxonBuildException(
"The docker image could not be built, client timed out."
)
示例10: test_build_raise_timeout
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def test_build_raise_timeout(self, build_mock):
build_mock.side_effect = ReadTimeoutError(None, "foo", "error")
with self.assertRaises(PolyaxonBuildException):
build(
context=".",
destination="image_name:image_tag",
nocache=True,
max_retries=1,
sleep_interval=0,
)
示例11: test_push_raise_timeout
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def test_push_raise_timeout(self, build_mock, push_mock):
push_mock.side_effect = ReadTimeoutError(None, "foo", "error")
with self.assertRaises(PolyaxonBuildException):
build_and_push(
context=".",
destination="image_name:image_tag",
nocache=True,
max_retries=1,
sleep_interval=0,
)
assert build_mock.call_count == 1
示例12: _do_http_request
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def _do_http_request(self, retry, machines_cache, request_executor, method, path, fields=None, **kwargs):
some_request_failed = False
for i, base_uri in enumerate(machines_cache):
if i > 0:
logger.info("Retrying on %s", base_uri)
try:
response = request_executor(method, base_uri + path, fields=fields, **kwargs)
response.data.decode('utf-8')
self._check_cluster_id(response)
if some_request_failed:
self.set_base_uri(base_uri)
self._refresh_machines_cache()
return response
except (HTTPError, HTTPException, socket.error, socket.timeout) as e:
self.http.clear()
# switch to the next etcd node because we don't know exactly what happened,
# whether the key didn't received an update or there is a network problem.
if not retry and i + 1 < len(machines_cache):
self.set_base_uri(machines_cache[i + 1])
if (isinstance(fields, dict) and fields.get("wait") == "true" and
isinstance(e, (ReadTimeoutError, ProtocolError))):
logger.debug("Watch timed out.")
raise etcd.EtcdWatchTimedOut("Watch timed out: {0}".format(e), cause=e)
logger.error("Request to server %s failed: %r", base_uri, e)
logger.info("Reconnection allowed, looking for another server.")
if not retry:
raise etcd.EtcdException('{0} {1} request failed'.format(method, path))
some_request_failed = True
raise etcd.EtcdConnectionFailed('No more machines in the cluster')
示例13: http_request
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def http_request(method, url, **kwargs):
if url == 'http://localhost:2379/timeout':
raise ReadTimeoutError(None, None, None)
ret = MockResponse()
if url == 'http://localhost:2379/v2/machines':
ret.content = 'http://localhost:2379,http://localhost:4001'
elif url == 'http://localhost:4001/v2/machines':
ret.content = ''
elif url != 'http://localhost:2379/':
raise socket.error
return ret
示例14: perform_request
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
url = self.url_prefix + url
if params:
url = '%s?%s' % (url, urlencode(params))
full_url = self.host + url
start = time.time()
try:
kw = {}
if timeout:
kw['timeout'] = timeout
# in python2 we need to make sure the url and method are not
# unicode. Otherwise the body will be decoded into unicode too and
# that will fail (#133, #201).
if not isinstance(url, str):
url = url.encode('utf-8')
if not isinstance(method, str):
method = method.encode('utf-8')
response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw)
duration = time.time() - start
raw_data = response.data.decode('utf-8')
except Exception as e:
self.log_request_fail(method, full_url, url, body, time.time() - start, exception=e)
if isinstance(e, UrllibSSLError):
raise SSLError('N/A', str(e), e)
if isinstance(e, ReadTimeoutError):
raise ConnectionTimeout('TIMEOUT', str(e), e)
raise ConnectionError('N/A', str(e), e)
# raise errors based on http status codes, let the client handle those if needed
if not (200 <= response.status < 300) and response.status not in ignore:
self.log_request_fail(method, full_url, url, body, duration, response.status, raw_data)
self._raise_error(response.status, raw_data)
self.log_request_success(method, full_url, url, body, response.status,
raw_data, duration)
return response.status, response.getheaders(), raw_data
示例15: test_retry_generator
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ReadTimeoutError [as 别名]
def test_retry_generator(exc, in_init, retry_times):
def simplegen():
yield "log line"
my_args = ('some', 'new')
if not in_init:
my_kwargs = {'one': 'first', 'two': 'second', 'retry_times': retry_times}
else:
my_kwargs = {'one': 'first', 'two': 'second'}
if in_init:
t = DockerTasker(retry_times=retry_times)
else:
t = DockerTasker()
(flexmock(time)
.should_receive('sleep')
.and_return(None))
if not exc:
cr = CommandResult()
cr._error = "cmd_error"
cr._error_detail = {"message": "error_detail"}
if exc == APIError:
error_message = 'api_error'
response = flexmock(content=error_message, status_code=408)
(flexmock(atomic_reactor.util)
.should_receive('wait_for_command')
.times(retry_times + 1)
.and_raise(APIError, error_message, response))
elif exc == ProtocolError:
error_message = 'protocol_error'
(flexmock(atomic_reactor.util)
.should_receive('wait_for_command')
.times(retry_times + 1)
.and_raise(ProtocolError, error_message))
elif exc == ReadTimeoutError:
pool = 'pool'
message = 'read_timeout_error'
error_message = '{}: {}'.format(pool, message)
(flexmock(atomic_reactor.util)
.should_receive('wait_for_command')
.times(retry_times + 1)
.and_raise(ReadTimeoutError, pool, 'url', message))
else:
(flexmock(atomic_reactor.util)
.should_receive('wait_for_command')
.times(retry_times + 1)
.and_return(cr))
error_message = 'cmd_error'
if retry_times >= 0:
with pytest.raises(RetryGeneratorException) as ex:
t.retry_generator(lambda *args, **kwargs: simplegen(),
*my_args, **my_kwargs)
assert repr(error_message) in repr(ex.value)
else:
t.retry_generator(lambda *args, **kwargs: simplegen(),
*my_args, **my_kwargs)