Last active
January 6, 2023 00:48
-
-
Save bcantoni/e6f481002f483ab310d45b3726a640f2 to your computer and use it in GitHub Desktop.
A simple Python script to check a web link and report back on the redirects encountered
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" Check web URL and list all redirections """ | |
import argparse | |
import requests | |
import sys | |
import time | |
parser = argparse.ArgumentParser(description='Check redirect chain for a web URL') | |
parser.add_argument('URL', help='URL to check') | |
args = parser.parse_args() | |
url = args.URL | |
t0 = time.time() | |
req = requests.head(url, allow_redirects=True) | |
if req.status_code != 200: | |
print "Sorry could not find that URL: {code}".format(code=req.status_code) | |
sys.exit(1) | |
space = '' | |
print url | |
for r in req.history: | |
print "{sp}--> {code}: {new_url}".format(sp=space, code=r.status_code, | |
new_url=r.headers['location']) | |
space += " " | |
t1 = time.time() | |
print "{n} redirect(s) in {t:8.4f} seconds".format(n=len(req.history), t=t1-t0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment