本文整理汇总了Python中requests.sessions方法的典型用法代码示例。如果您正苦于以下问题:Python requests.sessions方法的具体用法?Python requests.sessions怎么用?Python requests.sessions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests
的用法示例。
在下文中一共展示了requests.sessions方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import requests [as 别名]
# 或者: from requests import sessions [as 别名]
def __init__(self, config):
from .configuration import Configuration
if not isinstance(config, Configuration):
raise ValueError("'config' %r must be a Configuration instance" % config)
if not config.service_endpoint:
raise AttributeError("'config.service_endpoint' must be set")
self.config = config
self._session_pool_size = self.SESSION_POOLSIZE
# Autodetect authentication type if necessary
if self.config.auth_type is None:
self.config.auth_type = self.get_auth_type()
# Try to behave nicely with the remote server. We want to keep the connection open between requests.
# We also want to re-use sessions, to avoid the NTLM auth handshake on every request. We must know the
# authentication method to create a session pool.
self._session_pool = self._create_session_pool()
self._session_pool_lock = Lock()
示例2: close
# 需要导入模块: import requests [as 别名]
# 或者: from requests import sessions [as 别名]
def close(self):
log.debug('Server %s: Closing sessions', self.server)
while True:
try:
self._session_pool.get(block=False).close()
except Empty:
break
示例3: _create_session_pool
# 需要导入模块: import requests [as 别名]
# 或者: from requests import sessions [as 别名]
def _create_session_pool(self):
# Create a pool to reuse sessions containing connections to the server
session_pool = LifoQueue()
for _ in range(self._session_pool_size):
session_pool.put(self.create_session(), block=False)
return session_pool
示例4: get_session
# 需要导入模块: import requests [as 别名]
# 或者: from requests import sessions [as 别名]
def get_session(self):
_timeout = 60 # Rate-limit messages about session starvation
while True:
try:
log.debug('Server %s: Waiting for session', self.server)
session = self._session_pool.get(timeout=_timeout)
log.debug('Server %s: Got session %s', self.server, session.session_id)
session.usage_count += 1
return session
except Empty:
# This is normal when we have many worker threads starving for available sessions
log.debug('Server %s: No sessions available for %s seconds', self.server, _timeout)
示例5: release_session
# 需要导入模块: import requests [as 别名]
# 或者: from requests import sessions [as 别名]
def release_session(self, session):
# This should never fail, as we don't have more sessions than the queue contains
log.debug('Server %s: Releasing session %s', self.server, session.session_id)
if self.MAX_SESSION_USAGE_COUNT and session.usage_count > self.MAX_SESSION_USAGE_COUNT:
log.debug('Server %s: session %s usage exceeded limit. Discarding', self.server, session.session_id)
session = self.renew_session(session)
try:
self._session_pool.put(session, block=False)
except Full:
log.debug('Server %s: Session pool was already full %s', self.server, session.session_id)
示例6: clear_cache
# 需要导入模块: import requests [as 别名]
# 或者: from requests import sessions [as 别名]
def clear_cache(mcs):
for key, protocol in mcs._protocol_cache.items():
if isinstance(protocol, Exception):
continue
service_endpoint = key[0]
log.debug("Service endpoint '%s': Closing sessions", service_endpoint)
protocol.close()
mcs._protocol_cache.clear()
示例7: __init__
# 需要导入模块: import requests [as 别名]
# 或者: from requests import sessions [as 别名]
def __init__(self):
self.session = requests.sessions.Session()