Building CLI to check status of URL using Python
Last Updated :
28 Jul, 2021
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.
Stepwise Implementation
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
Python3
import sys
import requests
from http.client import responses
import validators
if len(sys.argv) < 2:
print('You need to specify atleast one URL')
sys.exit()
def main():
# main function
if __name__ == "__main__":
main()
Output:
error need at least 1 argument
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
Python3
def usage():
print("\nUsage:\nSingle URL: urlcheck <url>\
nMultiple URL's: urlcheck <url1> <url2> ... <urln>\n")
if sys.argv[1]=="help":
usage()
sys.exit()
Output:
help commandStep 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.
Python3
def main():
n = len(sys.argv)
for i in range(1, n):
url = sys.argv[i]
if validators.url(url) is True:
status = requests.head(url).status_code
try:
print(url, status, responses[status], "\n")
except:
print(url, status, "Not an Standard HTTP Response code\n")
else:
print(url, "Not an valid URL\n")
continue
Output:
CLI verifying URL status
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.
For Windows:
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
source ~/.profile
Below is the Complete Implementation:
Python3
import sys
import requests
from http.client import responses
import validators
if len(sys.argv) < 2:
print('You need to specify at least url')
sys.exit()
def usage():
print("\nUsage:\nSingle URL: urlcheck <url>\nMultiple\
URL's: urlcheck <url1> <url2> ... <urln>\n")
if sys.argv[1]=="help":
usage()
sys.exit()
def main():
n = len(sys.argv)
print("\n")
for i in range(1, n):
url = sys.argv[i]
if validators.url(url) is True:
status = requests.head(url).status_code
try:
print(url, status,responses[status], "\n")
except:
print(url, status, "Not an Standard HTTP Response code\n")
else:
print(url, "Not an valid URL\n")
continue
if __name__ == "__main__":
main()
Output:
final
Similar Reads
Python | How to shorten long URLs using Bitly API
Bitly is used to shorten, brand, share, or retrieve data from links programmatically. In this article, we'll see how to shorten URLs using Bitly API. Below is a working example to shorten a URL using Bitly API. Step #1: Install Bitly API using git git clone https://github.com/bitly/bitly-api-python.
2 min read
How to get current_url using Selenium in Python?
While doing work with selenium many URL get opened and redirected in order to keeping track of URL current_url method is used. The current_url method is used to retrieve the URL of the webpage the user is currently accessing. It gives the URL of the current webpage loaded by the driver in selenium.
2 min read
Fetch JSON URL Data and Store in Excel using Python
In this article, we will learn how to fetch the JSON data from a URL using Python, parse it, and store it in an Excel file. We will use the Requests library to fetch the JSON data and Pandas to handle the data manipulation and export it to Excel.Fetch JSON data from URL and store it in an Excel file
3 min read
How to Check Loading Time of Website using Python
In this article, we will discuss how we can check the website's loading time. Do you want to calculate how much time it will take to load your website? Then, what you must need to exactly do is subtract the time obtained passed since the epoch from the time obtained after reading the whole website.
3 min read
Building A Weather CLI Using Python
Trying to know the weather is something that we all do, every day. But have you ever dreamed of making a tool on your own that can do the same? If yes, then this article is for you. In this article, we will look at a step-by-step guide on how to build a Weather CLI using OpenWeather API. What is Ope
4 min read
Python | Sorting URL on basis of Top Level Domain
Given a list of URL, the task is to sort the URL in the list based on the top-level domain. A top-level domain (TLD) is one of the domains at the highest level in the hierarchical Domain Name System of the Internet. Example - org, com, edu. This is mostly used in a case where we have to scrap the pa
3 min read
Python Tweepy â Getting the ID of a status
In this article we will see how we can get the ID of a status. Each status or tweet has a unique ID. The id attribute of the Status object provides us with the ID of the status/tweet. Identifying the ID in the GUI : In the above mentioned status, the ID can be found in the URL, the ID here is : 1271
2 min read
Parsing and Processing URL using Python - Regex
Prerequisite: Regular Expression in Python URL or Uniform Resource Locator consists of many information parts, such as the domain name, path, port number etc. Any URL can be processed and parsed using Regular Expression. So for using Regular Expression we have to use re library in Python. Example: U
3 min read
Get all text of the page using Selenium in Python
As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient A
3 min read
Create API Tester using Python Requests Module
Prerequisites: Python Requests module, API In this article, we will discuss the work process of the Python Requests module by creating an API tester. API stands for Application Programming Interface (main participant of all the interactivity). It is like a messenger that takes our requests to a syst
3 min read