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

Viewed 26

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.
2 Answers

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

Related