Skip to content

Instantly share code, notes, and snippets.

Last active May 30, 2017
What would you like to do?
A simple Python script to check a web link and report back on the redirects encountered
#!/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)
space = ''
print url
for r in req.history:
print "{sp}--> {code}: {new_url}".format(sp=space, code=r.status_code,
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