本文整理汇总了Python中requests.packages.urllib3.util.Retry方法的典型用法代码示例。如果您正苦于以下问题:Python util.Retry方法的具体用法?Python util.Retry怎么用?Python util.Retry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.packages.urllib3.util
的用法示例。
在下文中一共展示了util.Retry方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: is_retry
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def is_retry(self, method, status_code, has_retry_after=False):
""" Is this method/status code retryable? (Based on whitelists and control
variables such as the number of total retries to allow, whether to
respect the Retry-After header, whether this header is present, and
whether the returned status code is on the list of status codes to
be retried upon on the presence of the aforementioned header)
"""
if (self.total and
self.respect_retry_after_header and
has_retry_after and
(status_code in self.RETRY_AFTER_STATUS_CODES)):
return True
else:
return super().is_retry(method=method, status_code=status_code,
has_retry_after=has_retry_after)
示例2: __init__
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def __init__(self, config_d):
super().__init__(config_d)
self.base_url = config_d['base_url']
self.api_key = config_d['api_key']
self.verify_tls = config_d.get('verify_tls', True)
self.base_api_url = '{}/api/v1'.format(self.base_url)
# requests session setup
self.session = requests.Session()
if not self.verify_tls:
self.session.verify = False
self.session.headers.update({'user-agent': 'zentral/0.0.1',
'accept': 'application/json',
'authorization': self.api_key})
max_retries = Retry(total=3, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
self.session.mount(self.base_api_url,
requests.adapters.HTTPAdapter(max_retries=max_retries))
示例3: set_default_retries
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def set_default_retries(*args, **kwargs):
"""
This function installs a default retry mechanism to be used in requests
calls. The arguments are those of urllib3's `Retry` object (see
http://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.retry.Retry).
Examples:
# use 3 retries, for default conditions only (network errors/timeouts)
set_default_retries(total=3)
set_default_retries(3) # total can be given as a positional argument
# use 5 retries, and add 429 and 503 responses to the list of
# retryable conditions
set_default_retries(5, status_forcelist=[429, 503])
# use 5 retries with an exponential backoff factor of 1
set_default_retries(5, backoff_factor=1)
"""
global DEFAULT_RETRY_ARGS
DEFAULT_RETRY_ARGS = kwargs
if len(args) > 1:
ValueError("too many arguments")
elif len(args) == 1:
DEFAULT_RETRY_ARGS["total"] = args[0]
示例4: __init__
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def __init__(self, base_url=BASE_URL, login_url=LOGIN_URL, session=None):
self.base_url = base_url
self.login_url = login_url
self.url = base_url.rstrip('/') + '/mods'
self.session = session or requests.session()
adapter = HTTPAdapter(max_retries=Retry(status_forcelist=[500, 503]))
self.session.mount('https://', adapter)
self.session.mount('http://', adapter)
示例5: adapter
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def adapter(self):
retry = util.Retry(
connect=5,
total=10,
method_whitelist=["GET"],
status_forcelist=[500, 501, 502, 503],
)
return adapters.HTTPAdapter(max_retries=retry)
示例6: __init__
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def __init__(self, config_d):
self.business_unit_fact_key = config_d.get('business_unit_fact_key')
self.group_fact_keys = config_d.get('group_fact_keys') or []
self.extra_fact_keys = config_d.get('extra_fact_keys') or []
self.puppetboard_url = config_d.get('puppetboard_url')
# prepare requests session with connection settings
self.puppetdb_url = config_d["puppetdb_url"]
self.api_base_url = '{}/pdb/query/v4'.format(self.puppetdb_url)
self.puppetdb_timeout = config_d.get('puppetdb_timeout', 10)
self.session = requests.Session()
# headers
self.session.headers.update({
'user-agent': 'zentral/0.0.1',
'content-type': 'application/json',
'accept': 'application/json',
'accept-charset': 'utf-8'
})
# ca
puppetdb_ca = config_d.get("puppetdb_ca")
if puppetdb_ca:
self.session.verify = puppetdb_ca
# client cert
cert = config_d.get("puppetdb_cert")
key = config_d.get("puppetdb_key")
if cert and key:
self.session.cert = (cert, key)
elif cert or key:
raise PuppetDBError("Incomplete puppetDB configuration")
# max retries
max_retries = Retry(total=3, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
self.session.mount(
self.api_base_url,
requests.adapters.HTTPAdapter(max_retries=max_retries)
)
示例7: __init__
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def __init__(self, config_d):
super(InventoryClient, self).__init__(config_d)
self.base_url = 'https://%(account)s.monitoringclient.com' % config_d
self.base_api_url = '{}/v2.5'.format(self.base_url)
# requests session setup
self.session = requests.Session()
self.session.headers.update({'user-agent': 'zentral/0.0.1',
'accept': 'application/json'})
self.session.params = {'api_key': config_d['api_key']}
max_retries = Retry(total=3, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
self.session.mount(self.base_api_url,
requests.adapters.HTTPAdapter(max_retries=max_retries))
示例8: __init__
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def __init__(self, config_d):
super(InventoryClient, self).__init__(config_d)
self.base_url = 'http{}://{}'.format(config_d.get('secure', True) * 's', config_d['host'])
self.api_base_url = '{}/api'.format(self.base_url)
self.public_key, self.private_key = config_d['public_key'], config_d['private_key']
# requests session setup
self.session = requests.Session()
self.session.headers.update({'user-agent': 'zentral/0.0.1',
'accept': 'application/json',
'privatekey': self.private_key,
'publickey': self.public_key})
max_retries = Retry(total=3, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
self.session.mount(self.api_base_url,
requests.adapters.HTTPAdapter(max_retries=max_retries))
示例9: __init__
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def __init__(self, host, port, path, user, password, secret, business_unit=None, **kwargs):
self.host, self.path, self.port, self.secret, self.business_unit = host, path, port, secret, business_unit
self.base_url = "https://{}:{}".format(host, port)
self.api_base_url = "{}{}".format(self.base_url, path)
# requests session setup
self.session = requests.Session()
self.session.headers.update({'user-agent': 'zentral/0.0.1',
'accept': 'application/json'})
self.session.auth = (user, password)
max_retries = Retry(total=3, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
self.session.mount(self.api_base_url,
requests.adapters.HTTPAdapter(max_retries=max_retries))
self.mobile_device_groups = {}
self.reverse_computer_groups = {}
示例10: session
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def session(self):
if self.request_session is None:
self.request_session = requests.Session()
# This will allow 5 tries at a url, with an increasing backoff. Only applies to a specific set of codes
self.request_session.mount('https://', HTTPAdapter(
max_retries=Retry(
total=5,
status_forcelist=[429, 500, 502, 503, 504],
backoff_factor=1,
)
))
return self.request_session
示例11: session
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def session(self):
"""
:return: Request Session object with maximum 3 retries and 5 as back off factor.
"""
if self.request_session is None:
self.request_session = requests.Session()
# This will allow 3 tries, with a backoff that increases by 5 each time. Applied only to specified status codes
self.request_session.mount('https://', HTTPAdapter(
max_retries=Retry(
total=TOTAL_RETRIES,
status_forcelist=[429, 500, 502, 503],
backoff_factor=BACK_OFF_FACTOR,
)
))
return self.request_session
示例12: session
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def session(self):
"""
:return: Request Session object with maximum 3 retries and 5 as back off factor.
"""
if self.request_session is None:
self.request_session = requests.Session()
# This will allow 5 tries at a url, with an increasing backoff. Only applies to a specific set of codes
self.request_session.mount('https://', HTTPAdapter(
max_retries=Retry(
total=TOTAL_RETRIES,
status_forcelist=[429, 500, 502, 503],
backoff_factor=BACK_OFF_FACTOR,
)
))
return self.request_session
示例13: __init__
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def __init__(self, timeout=None, retries=None):
super(Session, self).__init__()
self.timeout = timeout
if retries is None:
retry = Retry(**DEFAULT_RETRY_ARGS)
elif isinstance(retries, int):
args = DEFAULT_RETRY_ARGS.copy()
args.pop("total", None)
retry = Retry(total=retries, **args)
elif isinstance(retries, dict):
retry = Retry(**retries)
self.mount("http://", HTTPAdapter(max_retries=retry))
self.mount("https://", HTTPAdapter(max_retries=retry))
示例14: pinned_session
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def pinned_session(pool_maxsize=8):
http_adapter = _SSLAdapter(pool_connections=4,
pool_maxsize=pool_maxsize,
max_retries=Retry(total=1,
method_whitelist=Retry.DEFAULT_METHOD_WHITELIST.union(['POST'])))
_session = requests.session()
_session.hooks = {'response': redact_token}
_session.mount('https://', http_adapter)
return _session
示例15: _get_requests_session
# 需要导入模块: from requests.packages.urllib3 import util [as 别名]
# 或者: from requests.packages.urllib3.util import Retry [as 别名]
def _get_requests_session():
global _session
if _session is None:
_session = Session()
# configure 5 retries for any requests that return 502 or 503 errors
# exponential backoff increases the delay for each retry iteration
_session.mount('https://', HTTPAdapter(max_retries=Retry(
total=5, backoff_factor=0.1, status_forcelist=[502, 503])))
_session.mount('http://', HTTPAdapter(max_retries=Retry(
total=5, backoff_factor=0.1, status_forcelist=[502, 503])))
return _session