本文整理汇总了Python中requests.adapters.HTTPAdapter方法的典型用法代码示例。如果您正苦于以下问题:Python adapters.HTTPAdapter方法的具体用法?Python adapters.HTTPAdapter怎么用?Python adapters.HTTPAdapter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.adapters
的用法示例。
在下文中一共展示了adapters.HTTPAdapter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: requests_retry_session
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [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: connect
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [as 别名]
def connect(self):
if self.session is None:
# Creating a session that can later be re-use to configure custom mechanisms
self.session = requests.Session()
# If we specified some retries, we provide a predefined retry-logic
if self.retries > 0:
adapter = HTTPAdapter(
max_retries=Retry(
total=self.retries,
backoff_factor=0.1,
status_forcelist=[500, 502, 503, 504],
)
)
for prefix in "http://", "https://":
self.session.mount(prefix, adapter)
else:
raise TransportAlreadyConnected("Transport is already connected")
示例3: requests_retry_session
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [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
示例4: get_requests_session
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [as 别名]
def get_requests_session(self):
requests_session = self._session.GetParameter("requests_session")
if requests_session == None:
# To make sure we can use the requests session in the threadpool we
# need to make sure that the connection pool can block. Otherwise it
# will raise when it runs out of connections and the threads will be
# terminated.
requests_session = requests.Session()
requests_session.mount("https://", adapters.HTTPAdapter(
pool_connections=10, pool_maxsize=300, max_retries=10,
pool_block=True))
requests_session.mount("http://", adapters.HTTPAdapter(
pool_connections=10, pool_maxsize=300, max_retries=10,
pool_block=True))
self._session.SetCache("requests_session", requests_session)
return requests_session
示例5: __init__
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [as 别名]
def __init__(self, endpoint, session_id=None, req_session=None, args=None):
self._endpoint = endpoint.rstrip('/')
self._session_id = session_id
self._args = args or dict()
# dict structure: {tileable_key -> graph_key, tileable_ids}
# dict value is a tuple object which records graph key and tileable id
self._executed_tileables = dict()
self._serial_type = None
self._pickle_protocol = pickle.HIGHEST_PROTOCOL
if req_session:
self._req_session = req_session
else:
import requests
from requests.adapters import HTTPAdapter
self._req_session = requests.Session()
self._req_session.mount('http://stackoverflow.com', HTTPAdapter(max_retries=5))
self._main()
示例6: get_sync_result
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [as 别名]
def get_sync_result(result_payload: dict, sleep_time: int) -> dict:
# создаём сессию
session = requests.Session()
# выставляем кол-во попыток подключения к серверу при ошибке
session.mount("http://", HTTPAdapter(max_retries=5))
session.mount("https://", HTTPAdapter(max_retries=5))
session.verify = False
while True:
captcha_response = session.post(get_result_url, json=result_payload).json()
if captcha_response["errorId"] == 0:
if captcha_response["status"] == "processing":
time.sleep(sleep_time)
else:
captcha_response.update({"taskId": result_payload["taskId"]})
session.close()
return captcha_response
else:
captcha_response.update({"taskId": result_payload["taskId"]})
session.close()
return captcha_response
示例7: retry_session
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [as 别名]
def retry_session(retries=3, backoff_factor=0.8, status_forcelist=(500, 502, 504)):
"""Add retry to Requests Session.
https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.retry.Retry
"""
session = Session()
retries = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
# mount all https requests
session.mount('https://', HTTPAdapter(max_retries=retries))
return session
示例8: get_retrying_requests_session
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [as 别名]
def get_retrying_requests_session(client_statuses=HTTP_CLIENT_STATUS_RETRY,
times=HTTP_MAX_RETRIES, delay=HTTP_BACKOFF_FACTOR,
method_whitelist=None, raise_on_status=True):
if _http_retries_disabled():
times = 0
retry = Retry(
total=int(times),
backoff_factor=delay,
status_forcelist=client_statuses,
method_whitelist=method_whitelist
)
# raise_on_status was added later to Retry, adding compatibility to work
# with newer versions and ignoring this option with older ones
if hasattr(retry, 'raise_on_status'):
retry.raise_on_status = raise_on_status
session = SessionWithTimeout()
session.mount('http://', HTTPAdapter(max_retries=retry))
session.mount('https://', HTTPAdapter(max_retries=retry))
return session
示例9: retry
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [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
示例10: __init__
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [as 别名]
def __init__(self, loginer=None, use_proxy=False):
self.loginer = loginer
self.use_proxy = use_proxy
if use_proxy:
self.proxy_pool = ProxyPool()
if len(self.proxy_pool) == 0:
self.use_proxy = False
self._cookies = None
self._headers = dict()
self._headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
self._headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
self._headers["Accept-Encoding"] = "gzip, deflate, sdch"
self._headers["Accept-Language"] = "zh-CN,zh;q=0.8"
self._request_retry = HTTPAdapter(max_retries=3)
cookie_dict = dict()
self._cookies = cookie_dict
# def init_loginer(self, account, password):
# self._cookies = self.loginer.login(account, password)
#
# def set_cookies(self, cookies):
# self._cookies = cookies
示例11: requests_retry_session
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [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
示例12: init_poolmanager
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [as 别名]
def init_poolmanager(self, connections, maxsize, block=DEFAULT_POOLBLOCK):
"""Initializes a urllib3 PoolManager. This method should not be called
from user code, and is only exposed for use when subclassing the
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
:param connections: The number of urllib3 connection pools to cache.
:param maxsize: The maximum number of connections to save in the pool.
:param block: Block when no free connections are available.
"""
# save these values for pickling
self._pool_connections = connections
self._pool_maxsize = maxsize
self._pool_block = block
self.poolmanager = AsyncPoolManager(num_pools=connections, maxsize=maxsize, block=block)
self.connections = []
示例13: _requests_retry_session
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [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
示例14: requests_retry_session
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [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
示例15: get_docker_client
# 需要导入模块: from requests import adapters [as 别名]
# 或者: from requests.adapters import HTTPAdapter [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]