本文整理汇总了Python中requests.packages.urllib3.util.retry.Retry方法的典型用法代码示例。如果您正苦于以下问题:Python retry.Retry方法的具体用法?Python retry.Retry怎么用?Python retry.Retry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.packages.urllib3.util.retry
的用法示例。
在下文中一共展示了retry.Retry方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: requests_retry_session
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(500, 502, 504),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
示例2: requests_retry_session
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def requests_retry_session(
retries=5,
backoff_factor=1,
status_forcelist=[401, 402, 403, 500, 502, 504],
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount("http://", adapter)
session.mount("https://", adapter)
return session
示例3: retry
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def retry(num=5):
""""retry connection.
define max tries num
if the backoff_factor is 0.1, then sleep() will sleep for
[0.1s, 0.2s, 0.4s, ...] between retries.
It will also force a retry if the status code returned is 500, 502, 503 or 504.
"""
s = requests.Session()
retries = Retry(total=num, backoff_factor=0.1,
status_forcelist=[500, 502, 503, 504])
s.mount('http://', HTTPAdapter(max_retries=retries))
return s
# CONSTANT
示例4: requests_retry_session
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def requests_retry_session(
retries=5,
backoff_factor=0.3,
status_forcelist=(502, 504),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
示例5: _requests_retry_session
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def _requests_retry_session(
self,
retries=3,
backoff_factor=0.3,
method_whitelist=['HEAD', 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'],
status_forcelist=(500, 502, 503, 504, 520, 524),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
status=retries,
method_whitelist=method_whitelist,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
示例6: requests_retry_session
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(502, 504),
session=None,
**kwargs
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
示例7: get_docker_client
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def get_docker_client(base_url=None, retry_read=config.DOCKER_MAX_READ_RETRIES,
retry_status_forcelist=(500,)):
client_key = (retry_read, retry_status_forcelist)
if client_key not in _DOCKER_CLIENTS:
client = docker.DockerClient(base_url=base_url or config.DOCKER_URL,
timeout=config.DOCKER_TIMEOUT)
retries = Retry(total=config.DOCKER_MAX_TOTAL_RETRIES,
connect=config.DOCKER_MAX_CONNECT_RETRIES,
read=retry_read,
method_whitelist=False,
status_forcelist=retry_status_forcelist,
backoff_factor=config.DOCKER_BACKOFF_FACTOR,
raise_on_status=False)
http_adapter = HTTPAdapter(max_retries=retries)
client.api.mount('http://', http_adapter)
_DOCKER_CLIENTS[client_key] = client
return _DOCKER_CLIENTS[client_key]
示例8: send
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def send(self, request, *args, **kwargs):
content_length_is_unknown = (request.body is not None
and 'Content-Length' not in request.headers)
if content_length_is_unknown:
# it seems that requests's HTTPAdapter does not perform
# retries in such a case, even though they were requested
# (see the source code of HTTPAdapter.send() in conjunction
# with urllib3.connectionpool.HTTPConnectionPool.urlopen()
# and urllib3.util.retry.Retry.increment()...) -- so here
# we raise an exception to prevent such a silent omission
# [we analyzed this for requests==2.21.0 and urllib3==1.24.1]
raise ValueError('non-zero `retries` has been specified and, '
'at the same time, Content-Length of the request '
'could not be determined (suggested solutions: '
'specify `data` whose length is discoverable, '
'or specify `retries=0`)')
return super(_HTTPAdapterForRetries, self).send(request, *args, **kwargs)
示例9: _init_session
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def _init_session(session, **kwargs):
if session is None:
if kwargs.get('asynchronous'):
session = FuturesSession(max_workers=kwargs.get('max_workers', 8))
else:
session = Session()
if kwargs.get('proxies'):
session.proxies = kwargs.get('proxies')
retries = Retry(
total=3,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
method_whitelist=["HEAD", "GET", "OPTIONS", "POST", "TRACE"])
session.mount('https://', TimeoutHTTPAdapter(
max_retries=retries,
timeout=kwargs.get('timeout', DEFAULT_TIMEOUT)))
# TODO: Figure out how to utilize this within the validate_response
# TODO: This will be a much better way of handling bad requests than
# TODO: what I'm currently doing.
# session.hooks['response'] = \
# [lambda response, *args, **kwargs: response.raise_for_status()]
session.headers.update({
"User-Agent": random.choice(USER_AGENT_LIST)
})
return session
示例10: retry_session
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def retry_session():
"""Get a Requests session that retries HTTP and HTTPS requests.
"""
# Adapted from:
# https://www.peterbe.com/plog/best-practice-with-retries-with-requests
session = requests.Session()
retry = Retry(
total=RETRY_COUNT,
read=RETRY_COUNT,
connect=RETRY_COUNT,
backoff_factor=RETRY_FACTOR,
status_forcelist=RETRY_STATUSES,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
示例11: __retry_session
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def __retry_session(self, retries=10, backoff_factor=0.3,
status_forcelist=(500, 502, 504),
session=None):
"""
Retry the connection using requests if it fails. Use this as a wrapper
to request from datapoint
"""
# requests.Session allows finer control, which is needed to use the
# retrying code
the_session = session or requests.Session()
# The Retry object manages the actual retrying
retry = Retry(total=retries, read=retries, connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist)
adapter = HTTPAdapter(max_retries=retry)
the_session.mount('http://', adapter)
the_session.mount('https://', adapter)
return the_session
示例12: get_simple_salesforce_connection
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def get_simple_salesforce_connection(project_config, org_config, api_version=None):
# Retry on long-running metadeploy jobs
retries = Retry(total=5, status_forcelist=(502, 503, 504), backoff_factor=0.3)
adapter = HTTPAdapter(max_retries=retries)
sf = simple_salesforce.Salesforce(
instance_url=org_config.instance_url,
session_id=org_config.access_token,
version=api_version or project_config.project__package__api_version,
)
try:
app = project_config.keychain.get_service("connectedapp")
client_name = app.client_id
except (ServiceNotValid, ServiceNotConfigured):
client_name = "CumulusCI/{}".format(__version__)
sf.headers.setdefault(CALL_OPTS_HEADER_KEY, "client={}".format(client_name))
sf.session.mount("http://", adapter)
sf.session.mount("https://", adapter)
return sf
示例13: test_client_request_timeout_when_connection_retry_configuration_specified
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def test_client_request_timeout_when_connection_retry_configuration_specified(self):
connection_policy = documents.ConnectionPolicy()
# making timeout 0 ms to make sure it will throw
connection_policy.RequestTimeout = 0
connection_policy.ConnectionRetryConfiguration = Retry(
total=3,
read=3,
connect=3,
backoff_factor=0.3,
status_forcelist=(500, 502, 504)
)
with self.assertRaises(Exception):
# client does a getDatabaseAccount on initialization, which will time out
cosmos_client.CosmosClient(CRUDTests.host,
{'masterKey': CRUDTests.masterKey},
connection_policy)
示例14: initialize_client_with_connection_retry_config
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def initialize_client_with_connection_retry_config(self, retries):
connection_policy = documents.ConnectionPolicy()
connection_policy.ConnectionRetryConfiguration = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=0.3,
status_forcelist=(500, 502, 504)
)
start_time = time.time()
try:
cosmos_client.CosmosClient("https://localhost:9999",
{'masterKey': CRUDTests.masterKey},
connection_policy)
self.fail()
except ConnectionError as e:
end_time = time.time()
return end_time - start_time
示例15: decode
# 需要导入模块: from requests.packages.urllib3.util import retry [as 别名]
# 或者: from requests.packages.urllib3.util.retry import Retry [as 别名]
def decode(key: str, string: str) -> str:
string = base64.urlsafe_b64decode(string.encode() + b'===')
string = string.decode('latin')
encoded_chars = []
for i in range(len(string)):
key_c = key[i % len(key)]
encoded_c = chr((ord(string[i]) - ord(key_c) + 256) % 256)
encoded_chars.append(encoded_c)
encoded_string = ''.join(encoded_chars)
return encoded_string
# e = encode('a key', 'a message')
# d = decode('a key', e)
# print([e])
# print([d])
# https://www.peterbe.com/plog/best-practice-with-retries-with-requests
# Provides a requests Session object with requests' Retry capabilities.
# TODO: may make numbers below configurable