TooManyRedirects in requests, not in urllib2

```
>>> url = 'http://fasel.soup.io/'
>>> len(urllib2.urlopen(url).read())
73331
>>> len(requests.get(url).content)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.7-intel/egg/requests/api.py", line 50, in get
  File "build/bdist.macosx-10.7-intel/egg/requests/api.py", line 38, in request
  File "build/bdist.macosx-10.7-intel/egg/requests/sessions.py", line 200, in request
  File "build/bdist.macosx-10.7-intel/egg/requests/models.py", line 515, in send
  File "build/bdist.macosx-10.7-intel/egg/requests/models.py", line 209, in _build_response
requests.exceptions.TooManyRedirects
```


I have same problem with some FaceBook pages.


This occurs only in case of HTTP 301 redirects. AFAIK, I guess this is because, upto version 0.7.5, the HTTPRedirectHandler in monkeys.py doesn't handle the exception. The chain which executes upon a HTTP 301 has nothing to do in the method responsible for it. You can add code to handle the 301 error and fetch content from the location pointed to by the response header. Correct me if i am wrong. 


Kenneth, since you asked, as of 0.11.1 this is still an issue:

```
>>> import requests, urllib2
>>> requests.__version__
'0.11.1'
>>> url = 'http://fasel.soup.io/'
>>> r = requests.get(url)
Apr 12 22:48:22: Starting new HTTP connection (1): fasel.soup.io
Apr 12 22:48:23: Starting new HTTP connection (1): www.soup.io
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/requests-0.11.1-py2.7.egg/requests/api.py", line 52, in get
    return request('get', url, **kwargs)
  File "/Library/Python/2.7/site-packages/requests-0.11.1-py2.7.egg/requests/api.py", line 40, in request
    return s.request(method=method, url=url, **kwargs)
  File "/Library/Python/2.7/site-packages/requests-0.11.1-py2.7.egg/requests/sessions.py", line 208, in request
    r.send(prefetch=prefetch)
  File "/Library/Python/2.7/site-packages/requests-0.11.1-py2.7.egg/requests/models.py", line 597, in send
    self._build_response(r)
  File "/Library/Python/2.7/site-packages/requests-0.11.1-py2.7.egg/requests/models.py", line 226, in _build_response
    raise TooManyRedirects()
TooManyRedirects
```


Testing this same URL on 0.13.5 does not generate a `TooManyRedirects`. I think this makes the issue resolved, unless someone can still reproduce it somewhere else.


Yeah, it's intermittent, which is the worst part. Still happens, but I noticed that it has to do with the headers I'm sending:

```
headers = {
    'User-Agent': 'NewsBlur Page Fetcher (%s subscriber%s) - %s '
                  '(Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) '
                  'AppleWebKit/534.48.3 (KHTML, like Gecko) Version/5.1 '
                  'Safari/534.48.3)' % (
        self.feed.num_subscribers,
        's' if self.feed.num_subscribers != 1 else '',
        settings.NEWSBLUR_URL
    ),
    'Connection': 'close',
}
try:
    response = requests.get(feed_link, headers=headers)
except requests.exceptions.TooManyRedirects:
    response = requests.get(feed_link)
```


/frownyface

I'm having real trouble reproducing the problem, even using your headers. How intermittent is intermittent? Can you give me an estimate of the frequency?


I no longer keep track of the intermittency, but I can tell you that I was able to repro manually two weeks ago by hitting these urls:

http://lifehacker.com
http://jalopnik.com
http://kotaku.com/pc/
http://habrahabr.ru/
http://www.interfluidity.com

The Gawker sites (those first three) started happening in the past 6 months when they moved to their weird URL redirect mechanism that I believe is part of their new comment system. Some of this oddity might be cookie based, as well.

On Jul 27, 2012, at 10:42 AM, Cory Benfield reply@reply.github.com wrote:

> /frownyface
> 
> I'm having real trouble reproducing the problem, even using your headers. How intermittent is intermittent? Can you give me an estimate of the frequency?
> 
> ---
> 
> Reply to this email directly or view it on GitHub:
> https://github.com/kennethreitz/requests/issues/337#issuecomment-7311451


@samuelclay make sure you're using the latest release. I can't reproduce any of these :)


I am using the current release, and I am receiving this error with www.freshdirect.com/api/login. I experienced it using the headers `"User-Agent": "PostmanRuntime/7.24.1",` and `'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'`. I have repeatedly increased the max redirect using `       self.session.max_redirects = 500`. I am not getting this problem when I change my user-agent using the [Requestly Chrome Extension](https://requestly.in)