Building CLI to check status of URL using Python
In this article, we will build a CLI(command-line interface) program to verify the status of a URL using Python. The python CLI takes one or more URLs as arguments and checks whether the URL is accessible (or)not.
Step 1: Setting up files and Installing requirements
First, create a directory named “urlcheck” and create a new file in the directory “urlcheck.py”(you can use any name for the directory and python file).
mkdir urlcheck && cd urlcheck touch urlcheck.py
Install Python requests and validators libraries:
pip3 install requests validators
Now create a base Python file importing all the required libraries
In the above code, the sys library provides functions and variables used to manipulate different parts of the Python runtime environment. It is used to take arguments as input by using argv module, the requests library is helpful for making HTTP connections, the responses module from the python HTTP client library provides a small description for HTTP response codes, the validators library is used for validating the URL
If the condition verifies that the user has passed at least one argument if the condition fails the program will exit with an error stating that the user needs to specify at least one URL
Step 2: Creating help function
The help function shows the available commands and acts as documentation for the command. It runs when help is passed as an argument
Step 3: Creating the main function
The main function is the function that runs when the script starts executing, main function contains all the logic for our CLI program.
In the above code variable “n” is used to get the number of arguments the user has specified. The for loop runs taking one argument at a time and loops until all the arguments have been passed the “URL” stores the given argument one at a time and validates when it’s a valid URL. It is not a valid URL the else block prints a warning and continues with the next iteration If it’s a valid URL the requests library makes an HTTP connection using HEAD method. The HEAD method is similar to GET but doesn’t contain any HTML in its response, then we store the status code of the response in “status” variable
At last, we try to print out the URL, its response status code, and a small description of the status code. If the status code is not a standard HTTP response status code(CDN’s use custom status codes), it prints out Not a Standard HTTP Response code.
Step 4: Creating custom CLI command
There are multiple ways to create custom CLI commands but we will use an alias because it’s simple to create and doesn’t need to make any changes to file permissions (or) changing directories.
You need to edit the file /C/’Program Files’/Git/etc/profile.d/aliases.sh and add the location for your python script
alias urlcheck="location to your python file"
For Mac and Linux:
you need to edit ~/.profile add location to your python script
alias urlcheck="python3 ~/urlcheck/urlcheck.py"
After saving .profile run the below command to make sure the alias gets updates
Below is the Complete Implementation:
Please Login to comment...