Last active
April 27, 2021 05:35
-
-
Save ntanya/816cba067f0e0dccc524 to your computer and use it in GitHub Desktop.
Python script to check HTTP status and redirect chains
This file contains hidden or 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 | |
def get_status_code(url): | |
try: | |
r = requests.get(url) | |
print "Processing " + url | |
if len(r.history) > 0: | |
chain = "" | |
code = r.history[0].status_code | |
final_url = r.url | |
for resp in r.history: | |
chain += resp.url + " | " | |
return str(code) + '\t' + str(len(r.history)) + '\t' + chain + '\t' + final_url + '\t' | |
else: | |
return str(r.status_code) + '\t\t\t\t' | |
except requests.ConnectionError: | |
print("Error: failed to connect.") | |
return '0\t\t\t\t' | |
input_file = 'urls.txt' | |
output_file = 'output.txt' | |
with open(output_file, 'w') as o_file: | |
o_file.write('URL\tStatus\tNumber of redirects\tRedirect Chain\tFinal URL\t\n') | |
f = open(input_file, "r") | |
lines = f.read().splitlines() | |
for line in lines: | |
code = get_status_code(line) | |
o_file.write(line + "\t" + str(code) + "\t\n") | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment