(self, resp, req, stream=False, timeout=None,
                          verify=True, cert=None, proxies=None, **adapter_kwargs)
Receives a Response. Returns a generator of Responses.
Receives a Response. Returns a generator of Responses.
Receives a Response . Returns a generator of Responses .
def resolve_redirects(self, resp, req, stream=False, timeout=None,
                          verify=True, cert=None, proxies=None, **adapter_kwargs):
        """Receives a Response. Returns a generator of Responses."""

        i = 0
        hist = [] # keep track of history

        while resp.is_redirect:
            prepared_request = req.copy()

            if i > 0:
                # Update history and keep track of redirects.
                hist.append(resp)
                new_hist = list(hist)
                resp.history = new_hist

            try:
                resp.content  # Consume socket so it can be released
            except (ChunkedEncodingError, ContentDecodingError, RuntimeError):
                resp.raw.read(decode_content=False)

            if i >= self.max_redirects:
                raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)

            # Release the connection back into the pool.
            resp.close()

            url = resp.headers['location']
            method = req.method

            # Handle redirection without scheme (see: RFC 1808 Section 4)
            if url.startswith('//'):
                parsed_rurl = urlparse(resp.url)
                url = '%s:%s' % (parsed_rurl.scheme, url)

            # The scheme should be lower case...
            parsed = urlparse(url)
            url = parsed.geturl()

            # Facilitate relative 'location' headers, as allowed by RFC 7231.
            # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
            # Compliant with RFC3986, we percent encode the url.
            if not parsed.netloc:
                url = urljoin(resp.url, requote_uri(url))
            else:
                url = requote_uri(url)

            prepared_request.url = to_native_string(url)
            # Cache the url, unless it redirects to itself.
            if resp.is_permanent_redirect and req.url != prepared_request.url:
                self.redirect_cache[req.url] = prepared_request.url

            # http://tools.ietf.org/html/rfc7231#section-6.4.4
            if (resp.status_code == codes.see_other and
                    method != 'HEAD'):
                method = 'GET'

            # Do what the browsers do, despite standards...
            # First, turn 302s into GETs.
            if resp.status_code == codes.found and method != 'HEAD':
                method = 'GET'

            # Second, if a POST is responded to with a 301, turn it into a GET.
            # This bizarre behaviour is explained in Issue 1704.
            if resp.status_code == codes.moved and method == 'POST':
                method = 'GET'

            prepared_request.method = method

            # https://github.com/kennethreitz/requests/issues/1084
            if resp.status_code not in (codes.temporary_redirect, codes.permanent_redirect):
                if 'Content-Length' in prepared_request.headers:
                    del prepared_request.headers['Content-Length']

                prepared_request.body = None

            headers = prepared_request.headers
            try:
                del headers['Cookie']
            except KeyError:
                pass

            # Extract any cookies sent on the response to the cookiejar
            # in the new request. Because we've mutated our copied prepared
            # request, use the old one that we haven't yet touched.
            extract_cookies_to_jar(prepared_request._cookies, req, resp.raw)
            prepared_request._cookies.update(self.cookies)
            prepared_request.prepare_cookies(prepared_request._cookies)

            # Rebuild auth and proxy information.
            proxies = self.rebuild_proxies(prepared_request, proxies)
            self.rebuild_auth(prepared_request, resp)

            # Override the original request.
            req = prepared_request

            resp = self.send(
                req,
                stream=stream,
                timeout=timeout,
                verify=verify,
                cert=cert,
                proxies=proxies,
                allow_redirects=False,
                **adapter_kwargs
            )

            extract_cookies_to_jar(self.cookies, prepared_request, resp.raw)

            i += 1
            yield resp
def resolve_redirects ( self , resp , req , stream = False , timeout = None , verify = True , cert = None , proxies = None , * * adapter_kwargs ) : i = 0 hist = [ ] # keep track of history while resp . is_redirect : prepared_request = req . copy ( ) if i > 0 : # Update history and keep track of redirects. hist . append ( resp ) new_hist = list ( hist ) resp . history = new_hist try : resp . content # Consume socket so it can be released except ( ChunkedEncodingError , ContentDecodingError , RuntimeError ) : resp . raw . read ( decode_content = False ) if i >= self . max_redirects : raise TooManyRedirects ( 'Exceeded %s redirects.' % self . max_redirects ) # Release the connection back into the pool. resp . close ( ) url = resp . headers [ 'location' ] method = req . method # Handle redirection without scheme (see: RFC 1808 Section 4) if url . startswith ( '//' ) : parsed_rurl = urlparse ( resp . url ) url = '%s:%s' % ( parsed_rurl . scheme , url ) # The scheme should be lower case... parsed = urlparse ( url ) url = parsed . geturl ( ) # Facilitate relative 'location' headers, as allowed by RFC 7231. # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource') # Compliant with RFC3986, we percent encode the url. if not parsed . netloc : url = urljoin ( resp . url , requote_uri ( url ) ) else : url = requote_uri ( url ) prepared_request . url = to_native_string ( url ) # Cache the url, unless it redirects to itself. if resp . is_permanent_redirect and req . url != prepared_request . url : self . redirect_cache [ req . url ] = prepared_request . url # http://tools.ietf.org/html/rfc7231#section-6.4.4 if ( resp . status_code == codes . see_other and method != 'HEAD' ) : method = 'GET' # Do what the browsers do, despite standards... # First, turn 302s into GETs. if resp . status_code == codes . found and method != 'HEAD' : method = 'GET' # Second, if a POST is responded to with a 301, turn it into a GET. # This bizarre behaviour is explained in Issue 1704. if resp . status_code == codes . moved and method == 'POST' : method = 'GET' prepared_request . method = method # https://github.com/kennethreitz/requests/issues/1084 if resp . status_code not in ( codes . temporary_redirect , codes . permanent_redirect ) : if 'Content-Length' in prepared_request . headers : del prepared_request . headers [ 'Content-Length' ] prepared_request . body = None headers = prepared_request . headers try : del headers [ 'Cookie' ] except KeyError : pass # Extract any cookies sent on the response to the cookiejar # in the new request. Because we've mutated our copied prepared # request, use the old one that we haven't yet touched. extract_cookies_to_jar ( prepared_request . _cookies , req , resp . raw ) prepared_request . _cookies . update ( self . cookies ) prepared_request . prepare_cookies ( prepared_request . _cookies ) # Rebuild auth and proxy information. proxies = self . rebuild_proxies ( prepared_request , proxies ) self . rebuild_auth ( prepared_request , resp ) # Override the original request. req = prepared_request resp = self . send ( req , stream = stream , timeout = timeout , verify = verify , cert = cert , proxies = proxies , allow_redirects = False , * * adapter_kwargs ) extract_cookies_to_jar ( self . cookies , prepared_request , resp . raw ) i += 1 yield resp