Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting a redirect loop with this python program?

I’m trying to get a url using python requests. requests.get('https://google.com') works fine but then I add my custom headers and get a redirect loop, why?

import requests
s = requests.Session()
s.headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", 
    "Accept-Encoding": "gzip, deflate", 
    "Accept-Language": "en-US,en", 
    "Host": "google.com", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36", 
    "X-Amzn-Trace-Id": "Root=1-63261f60-6a62c4934cad2e126b4bdd8c"
  }
s.get('https://google.com')

I then get this error:

raise TooManyRedirects(
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you should set Host to www.google.com

import requests
s = requests.Session()
s.headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", 
    "Accept-Encoding": "gzip, deflate", 
    "Accept-Language": "en-US,en", 
    "Host": "www.google.com", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36", 
    "X-Amzn-Trace-Id": "Root=1-63261f60-6a62c4934cad2e126b4bdd8c"
  }
resp = s.get('https://www.google.com')

google server checks your header host, because it is not equal to www.google.com it said redirect to the www.google.com and because you never change it in your headers so get stuck in redirect loop

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading