Skip to content
Related Articles

Related Articles

Improve Article

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 command

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.

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :