add 'max_retries' configuration
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent c48f0d9 · Browse files · 2 files·+23 added−48 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+23 added−48 removed
- other:+23 added−48 removed
Provenance
- Commit
6a51f6b- Tree
7a781f0- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
c48f0d9
2 files changed+23 added−48 removed
modified:requests/defaults.py+1 added
36 unmodified lines37 defaults['timeout_fallback'] = True38 # defaults['keep_alive'] = True39 defaults['pool_connections'] = 1040 defaults['pool_maxsize'] = 141+defaults['max_retries'] = 0modified:requests/models.py+22 added-48 removed
12 unmodified lines13 14 from urlparse import urlparse, urlunparse, urljoin15 from datetime import datetime16 17+18 from .auth import dispatch as auth_dispatch19 from .hooks import dispatch_hook20 from .structures import CaseInsensitiveDict21 from .status_codes import codes21-from .exceptions import Timeout, URLRequired, TooManyRedirects, HTTPError22-22+from .packages.urllib3.exceptions import MaxRetryError23+from .exceptions import (24+ Timeout, URLRequired, TooManyRedirects, HTTPError, ConnectionError)25 from .utils import (26 dict_from_cookiejar, get_unicode_from_response,27 stream_decode_response_unicode, decode_gzip, stream_decode_gzip)28 101 unmodified lines130 def __repr__(self):131 return '<Request [%s]>' % (self.method)132 133 132- # def _get_opener(self):133- # """Creates appropriate opener object for urllib2."""134-135- # _handlers = []136-137- # if self.cookies is not None:138- # _handlers.append(urllib2.HTTPCookieProcessor(self.cookies))139-140- # if self.proxies:141- # _handlers.append(urllib2.ProxyHandler(self.proxies))142-143- # _handlers.append(HTTPRedirectHandler)144-145- # if not _handlers:146- # return urllib2.urlopen147-148- # if self.data or self.files:149- # _handlers.extend(get_handlers())150-151- # opener = urllib2.build_opener(*_handlers)152-153- # if self.headers:154- # # Allow default headers in the opener to be overloaded155- # normal_keys = [k.capitalize() for k in self.headers]156- # for key, val in opener.addheaders[:]:157- # if key not in normal_keys:158- # continue159- # # Remove it, we have a value to take its place160- # opener.addheaders.remove((key, val))161-162- # return opener.open163-164-134 def _build_response(self, resp, is_error=False):135 """Build internal :class:`Response <Response>` object136 from given response.137 """49 unmodified lines187 ('location' in r.headers) and188 ((r.status_code is codes.see_other) or (self.allow_redirects))189 ):190 222- # r.raw.close()223-191 if not len(history) < self.config.get('max_redirects'):192 raise TooManyRedirects()193 194 history.append(r)173 unmodified lines368 369 # Attach Cookie header to request.370 self.headers['Cookie'] = cookie_header371 372+ try:405- # Create the connection.406- r = conn.urlopen(407- method=self.method,408- url=url,409- body=body,410- headers=self.headers,411- redirect=False,412- assert_same_host=False,413- preload_content=False,414- decode_content=False415- )373+ # Create the connection.374+ r = conn.urlopen(375+ method=self.method,376+ url=url,377+ body=body,378+ headers=self.headers,379+ redirect=False,380+ assert_same_host=False,381+ preload_content=False,382+ decode_content=False,383+ retries=self.config.get('max_retries', 0)384+ )385+ except MaxRetryError, e:386+ if self.config.get('safe_mode', False):387+ pass388+ raise ConnectionError(e)389 390+391 self._build_response(r)392 393 # Response manipulation hook.394 self.response = dispatch_hook('response', self.hooks, self.response)152 unmodified lines