Search code examples
pythonpython-3.xurllib3

Python urllib3 too many redirects


I learn Python now and I want to create simple tool to opening few sites. I have next code:

#!/usr/bin/python
import urllib3, ssl, certifi
from urllib3 import Retry, Timeout

def openurl(url, method = "get"):
    retries = Retry(connect=500, read=2, redirect=500)
    http = urllib3.PoolManager(
        cert_reqs = 'CERT_REQUIRED',
        ca_certs = certifi.where(),
        retries = retries
    )
    con = urllib3.connection_from_url(url)
    r = con.request(method, '/trades');

openurl("http://www.steamgifts.com")

But on this site script returns Caused by ResponseError('too many redirects',)

I try to fix this by Retry(connect=500, read=2, redirect=500) but I don't see changes.


Solution

  • The web site is blocking some user agents. You can pretend to be a bona fide webbrowser and not a sneaky hacker by setting your own http headers for the request. I'm not familiar with urllib3, but with requests it's very straightforward.

    >>> requests.get('http://www.steamgifts.com/trades')
    <Response [403]>
    
    >>> requests.get('http://www.steamgifts.com/trades', 
        headers={'User-Agent': 'internet explorer or something'})
    <Response [200]>