Skip to content

Instantly share code, notes, and snippets.

@clementi
Last active July 9, 2018 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clementi/0f70fe9415d894afb7b0b854f7896c89 to your computer and use it in GitHub Desktop.
Save clementi/0f70fe9415d894afb7b0b854f7896c89 to your computer and use it in GitHub Desktop.
Print out the redirection chain of a URL. Doesn't deal with loops.
import requests
import sys
from colorama import init, Fore, Style
def main():
init() # colorama
if len(sys.argv) < 2:
raise Exception("URL required.")
url = sys.argv[1]
number = 1
while url != None:
resp = requests.get(url, allow_redirects=False)
if 'Location' in resp.headers.keys():
location = resp.headers['location']
number_fore_color = Fore.CYAN
redir_fore_color = Fore.GREEN if resp.status_code == 301 else Fore.YELLOW
print(f"{number_fore_color}{number}.{Style.RESET_ALL} {url} {redir_fore_color}-[{resp.status_code}]->{Style.RESET_ALL} {location}")
url = location
number += 1
else:
url = None
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment