0

In python I have:

session = requests.Session()
session.max_redirects = max_red
response = session.get(ip_address)

But how can I limit redirects to same domain only? I mean if the main requests was made to 192.168.1.1 then I want to allow redirections to 192.168.1.1, http://192.168.1.1, https://192.168.1.1, http://sub.192.168.1.1 etc...

I don't want to follow any external redirection, how can I do that in python?

algo
  • 1
  • 3
  • FWIW, `192.168.1.1` and `192.168.1.1.com` are two entirely different things. – deceze May 26 '23 at 06:13
  • `sub.192.168.1.1` is also an entirely different thing. There aren’t really any variations to IP addresses. If you started with a domain name, you could add subdomains to it. But an IP is an IP, end of story. – deceze May 26 '23 at 06:26
  • See this answer for some possible help. https://stackoverflow.com/questions/20475552/python-requests-library-redirect-new-url. – WombatPM May 26 '23 at 06:44
  • @deceze thank you again, so given a single response how can I check if it's the same IP I provided for initial request? – algo May 26 '23 at 07:01

1 Answers1

2

Note: this is an untested approach, but here for illustration/inspiration

I don't believe the requests library has any way of doing this automagically for you.

So, I propose you:

  1. Disable redirects (set allow_redirects=False on your request)
  2. Inspect the Response object to see if it was a redirect response (there are helper @property available!)
  3. Parse the redirect URL and make your decision as to whether to follow that redirect or not

Something like:

import requests
from urllib3.util import parse_url

intial_url = "http://192.168.1.1/"

response = requests.get(url=intial_url, allow_redirects=False)

# <https://github.com/psf/requests/blob/6e5b15d542a4e85945fd72066bb6cecbc3a82191/requests/models.py##L770-L775>
if response.is_redirect:
    redirect_url = response.headers["location"]

    if parse_url(intial_url).host == parse_url(redirect_url).host:
        # follow redirect?
        request.get(url=redirect_url, allow_redirects=False)

Obviously there's more to do (e.g. recursively follow redirects) but thought it might be useful anyhow

gvee
  • 16,732
  • 35
  • 50
  • 2
    P.S. rolling your own introduces risks that the standard `requests` library covers for you nicely e.g. maximum redirect recursion so proceed with caution ⚠️ – gvee May 26 '23 at 06:43