requests.packages.urllib3.util.Retry

Here are the examples of the python api requests.packages.urllib3.util.Retry taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

11 Examples 7

Example 1

Project: twine Source File: repository.py
    @staticmethod
    def _make_adapter_with_retries():
        retry = util.Retry(
            connect=5,
            total=10,
            method_whitelist=['GET'],
            status_forcelist=[500, 501, 502, 503],
        )
        return adapters.HTTPAdapter(max_retries=retry)

Example 2

Project: zentral Source File: jss.py
Function: init
    def __init__(self, config_d):
        super(InventoryClient, self).__init__(config_d)
        self.base_url = 'https://%(host)s:%(port)s' % config_d
        self.api_base_url = '%s%s' % (self.base_url, config_d['path'])
        # requests session setup
        self.session = requests.Session()
        self.session.headers.update({'user-agent': 'zentral/0.0.1',
                                     'accept': 'application/json'})
        self.session.auth = (config_d['user'], config_d['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))

Example 3

Project: zentral Source File: sal.py
    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))

Example 4

Project: zentral Source File: watchman.py
Function: init
    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.2'.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))

Example 5

Project: python-cloudant Source File: adapters.py
Function: init
    def __init__(self, retries=3, initialBackoff=0.25):
        super(Replay429Adapter, self).__init__(max_retries=Retry(
            # Configure the number of retries for status codes
            total=retries,
            # No retries for connect|read errors
            connect=0,
            read=0,
            # Allow retries for all the CouchDB HTTP method types
            method_whitelist=frozenset(['GET', 'HEAD', 'PUT', 'POST',
                                        'DELETE', 'COPY']),
            # Only retry for a 429 too many requests status code
            status_forcelist=[429],
            # Configure the start value of the doubling backoff
            backoff_factor=initialBackoff))

Example 6

Project: frappe Source File: __init__.py
def get_request_session(max_retries=3):
	from requests.packages.urllib3.util import Retry
	session = requests.Session()
	session.mount("http://", requests.adapters.HTTPAdapter(max_retries=Retry(total=5, status_forcelist=[500])))
	session.mount("https://", requests.adapters.HTTPAdapter(max_retries=Retry(total=5, status_forcelist=[500])))
	return session

Example 7

Project: tilequeue Source File: wof.py
def _make_requests_session_with_retries(max_retries):
    from requests.adapters import HTTPAdapter
    from requests.packages.urllib3.util import Retry

    s = requests.Session()
    a = HTTPAdapter(
        max_retries=Retry(
            total=max_retries,
            status_forcelist=[  # this is a list of statuses to consider to be
                                # an error and retry.
                429,  # Too many requests (i.e: back off)
                500,  # Generic internal server error
                502,  # Bad Gateway - i.e: upstream failure
                503,  # Unavailable, temporarily
                504,  # Gateway timeout
                522   # Origin connection timed out
            ],
            backoff_factor=1.0  # back off for 0s, 1s, 3s, 7s, etc... after
                                # each successive failure. (factor*(2^N-1))
        ))

    # use retry for both HTTP and HTTPS connections.
    s.mount('http://', a)
    s.mount('https://', a)

    return s

Example 8

Project: robotframework-requests Source File: RequestsKeywords.py
    def _create_session(
            self,
            alias,
            url,
            headers,
            cookies,
            auth,
            timeout,
            max_retries,
            backoff_factor,
            proxies,
            verify,
            debug,
            disable_warnings):
        """ Create Session: create a HTTP session to a server

        ``url`` Base url of the server

        ``alias`` Robot Framework alias to identify the session

        ``headers`` Dictionary of default headers

        ``auth`` List of username & password for HTTP Basic Auth

        ``timeout`` Connection timeout
        
        ``max_retries`` The maximum number of retries each connection should attempt.
        
        ``backoff_factor`` The pause between for each retry

        ``proxies`` Dictionary that contains proxy urls for HTTP and HTTPS communication

        ``verify`` Whether the SSL cert will be verified. A CA_BUNDLE path can also be provided.

        ``debug`` Enable http verbosity option more information
                https://docs.python.org/2/library/httplib.html#httplib.HTTPConnection.set_debuglevel
        
        ``disable_warnings`` Disable requests warning useful when you have large number of testcases                
        """

        self.builtin.log('Creating session: %s' % alias, 'DEBUG')
        s = session = requests.Session()
        s.headers.update(headers)
        s.auth = auth if auth else s.auth
        s.proxies = proxies if proxies else s.proxies

        try:
            max_retries = int(max_retries)
        except ValueError as err:
            raise ValueError("Error converting max_retries parameter: %s"   % err)        

        if max_retries > 0:
            http = requests.adapters.HTTPAdapter(max_retries=Retry(total=max_retries, backoff_factor=backoff_factor))
            https = requests.adapters.HTTPAdapter(max_retries=Retry(total=max_retries, backoff_factor=backoff_factor))
            
            # Disable requests warnings, useful when you have large number of testcase
            # you will observe drastical changes in Robot log.html and output.xml files size 
            if disable_warnings:
                logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
                logging.getLogger().setLevel(logging.ERROR)
                requests_log = logging.getLogger("requests")
                requests_log.setLevel(logging.ERROR)
                requests_log.propagate = True
                if not verify:
                    requests.packages.urllib3.disable_warnings()
            
            
            # Replace the session's original adapters
            s.mount('http://', http)
            s.mount('https://', https)

        # verify can be a Boolean or a String
        if isinstance(verify, bool):
            s.verify = verify
        elif isinstance(verify, str) or isinstance(verify, unicode):
            if verify.lower() == 'true' or verify.lower() == 'false':
                s.verify = self.builtin.convert_to_boolean(verify)
        else:
            # not a Boolean nor a String
            s.verify = verify

        # cant pass these into the Session anymore
        self.timeout = float(timeout) if timeout is not None else None
        self.cookies = cookies
        self.verify = verify if self.builtin.convert_to_boolean(verify) != True else None

        s.url = url

        # Enable http verbosity
        if int(debug) >= 1:
            self.debug = int(debug)
            httplib.HTTPConnection.debuglevel = self.debug

        self._cache.register(session, alias=alias)
        return session

Example 9

Project: ThreatExchange Source File: request.py
Function: build_session
    @classmethod
    def build_session(cls, retries=None):
        """
        Build custom requests session with retry capabilities.

        :param retries: Number of retries before stopping.
        :type retries: int
        :returns: requests session object
        """

        if retries is None:
            retries = 0
        session = requests.Session()
        session.mount('https://',
                      requests.adapters.HTTPAdapter(
                          max_retries=Retry(total=retries,
                                            status_forcelist=[500, 503]
                                            )
                      ))
        return session

Example 10

Project: zhihu-py3 Source File: common.py
def class_common_init(url_re, allowed_none=True, trailing_slash=True):
    def real(func):
        @functools.wraps(func)
        def wrapper(self, url, *args, **kwargs):
            if url is None and not allowed_none:
                raise ValueError('Invalid Url: ' + url)
            if url is not None:
                if url_re.match(url) is None:
                    raise ValueError('Invalid URL: ' + url)
                if not url.endswith('/') and trailing_slash:
                    url += '/'
            if 'session' not in kwargs.keys() or kwargs['session'] is None:
                kwargs['session'] = Session()
                kwargs['session'].mount('https://', Retry(5))
                kwargs['session'].mount('http://', Retry(5))
            self.soup = None
            return func(self, url, *args, **kwargs)

        return wrapper

    return real

Example 11

Project: kuma Source File: akismet.py
    def __init__(self):
        self.domain = settings.DOMAIN
        self.ssl = bool(getattr(settings, 'SECURE_PROXY_SSL_HEADER', False))
        self.session = Session()
        self.adapter = HTTPAdapter(
            max_retries=Retry(
                # number of total retries
                total=5,
                # retry once in case we can't connect to Akismet
                connect=1,
                # retry once in case we can't read the response from Akismet
                read=1,
                # retry once in case we're redirect by Akismet
                redirect=1,
                # definitely retry if Akismet is unwell
                status_forcelist=[500, 503])
        )
        self.session.mount('http://', self.adapter)
        self.session.mount('https://', self.adapter)
        self._verified = None