- This script checks a list of URLs and returns their redirect location and status code.
- The
check_redirects
function takesbase_url
andpaths
as arguments. - The script combines the base URL with each relative path using
urljoin
. - The script sends a GET request to each combined URL and prints the HTTP status code and final URL after any redirects, or indicates if there were no redirects.
- The main block checks for enough command-line arguments and calls
check_redirects
with the base URL and relative paths.
- Python 3 minimum
requests
library - this script will detact if this isn't installed and prompt the user to install it.
python <path to file>/check_redirects.py <base_url> <path1> <path2> ...
For a base URL of 'https://www.example.com' and the paths '/women/shoes' and '/men/shirts':
For a base URL of 'https://www.example.com' and the paths '/women/shoes' and '/men/shirts':
python Ashley_Code/URL_Redirect_Checker/check_redirects.py https://www.example.com /women/shoes /men/shirts
-------------------------------------------------------------------------------------
♡ Redirect Applied ♡
Full redirect chain
Original URL : https://www.example.com/dogs
-> Final URL : https://www.example.com/cute-dogs
Status code chain: 301 -> 200
-------------------------------------------------------------------------------------
Final status & URL: (200) https://www.example.com/cute-dogs
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
♡ No redirect applied ♡
Complete URL: https://www.example.com/cute-dogs
Status code : 200
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
♡ Redirect Applied ♡
Full redirect chain
Original URL : https://www.example.com/dogs
-> Final URL : https://www.example.com/cute-dogs
Status code chain: 301 -> 200
-------------------------------------------------------------------------------------
Final status & URL: (200) https://www.example.com/cute-dogs
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
* Error * 'requests' is not installed. Please install it using 'pip install requests'
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
* Error While Processing https://www.example.com/cute-dogs *
404 Client Error: XYZ
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
* This request has timed out *
https://www.example.com/cute-dogs
-------------------------------------------------------------------------------------
- Monday 8th of July 2024 ~
edit_outputs
Tidying up the output and adding in a timeout after 10 seconds with an error message. - Monday 8th of July 2024 ~
Headers
Adding common browser User-Agents, this acts more like curl (used my script IRL and the output compared to curl differed) - Wednesday 7th of August 2024 ~ Made the outputs pretty.
- Thursday 8th of August 2024 ~ Added more
Headers
, switched fromGET
toHEAD
to mimiccurl -IL
closer. Added arequests.Session
to maintain headers and cookies across requests - Wednesday 14th of August 2024 ~ Added
check_dependencies
for therequests
library, it will prompt the user to installrequirements.txt
if it is not installed. - Thursday 22nd of August 2024 ~ created a dynamic line separator function that bases the number of
-
on the width of the terminal.