Python requests.sessions() Examples

The following are 7 code examples of requests.sessions(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module requests , or try the search function .
Example #1
Source File: protocol.py    From exchangelib with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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() 
Example #2
Source File: protocol.py    From exchangelib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def close(self):
        log.debug('Server %s: Closing sessions', self.server)
        while True:
            try:
                self._session_pool.get(block=False).close()
            except Empty:
                break 
Example #3
Source File: protocol.py    From exchangelib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 
Example #4
Source File: protocol.py    From exchangelib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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) 
Example #5
Source File: protocol.py    From exchangelib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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) 
Example #6
Source File: protocol.py    From exchangelib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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() 
Example #7
Source File: udemy-dl.py    From udemy-dl-windows with The Unlicense 5 votes vote down vote up
def __init__(self):
        self.session = requests.sessions.Session()