Skip to content

Instantly share code, notes, and snippets.

@SimonMayerhofer
Last active July 4, 2024 10:16
Show Gist options
  • Save SimonMayerhofer/4f57943963ec337d0bad0509943636de to your computer and use it in GitHub Desktop.
Save SimonMayerhofer/4f57943963ec337d0bad0509943636de to your computer and use it in GitHub Desktop.
Python script to follow redirects and get final URL + route logging.
import requests
from urllib.parse import urlparse
import json
import traceback
import urllib3
# Suppress only the single InsecureRequestWarning from urllib3 needed
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def follow_redirects(url):
try:
route_log = []
session = requests.Session()
response = session.get(url, allow_redirects=True, verify=False)
# Get the history of redirects
history = response.history
for resp in history:
route_log.append(resp.url)
# Add the final URL to the route log
final_url = response.url
route_log.append(final_url)
# Get the domain of the final URL
parsed_final_url = urlparse(final_url)
final_domain = parsed_final_url.netloc
# Create the result JSON
result = {
"success": True,
"start_url": url,
"final_url": final_url,
"final_domain": final_domain,
"route_log": route_log
}
except requests.RequestException as e:
# Create the error result JSON
result = {
"success": False,
"error": {
"message": str(e),
"name": type(e).__name__,
"stack": traceback.format_exc(),
"code": e.response.status_code if e.response else None
}
}
except Exception as e:
# Catch any other exceptions and create the error result JSON
result = {
"success": False,
"error": {
"message": str(e),
"name": type(e).__name__,
"stack": traceback.format_exc(),
"code": None
}
}
return json.dumps(result, indent=4)
# Example usage
if __name__ == "__main__":
url = "https://google.com"
result_json = follow_redirects(url)
print(result_json)
@SimonMayerhofer
Copy link
Author

Example output:

{
    "success": true,
    "start_url": "https://google.com",
    "final_url": "https://www.google.com/",
    "final_domain": "www.google.com",
    "route_log": [
        "https://google.com/",
        "https://www.google.com/"
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment