Last active
July 9, 2018 22:09
-
-
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.
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
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