Python to Fetch the HTTP Status Code with Source Code

This script is used to fetch the status code of any request.

  • Input:
    • URL of a website or an API
  • Output:
    • Line 1 : Status code of the response followed by emoji (thumbsup/thumbsdown)
    • Line 2 : Relevant message about the status code. i.e. If status code corresponds to failure, then reason of failure would be shown in the message field.


  • emoji==0.6.0


  • This program uses and external dependency of “emoji” library to display the emojis in output.
  • This library can be installed easily by using the following command: pip install -r requirements.txt

How to Use:

  • Install the requirements.
  • Type the following command: python
  • A message asking URL/API would be displayed : Enter any url of choice and check the output

Source Code:

#Program to fetch the http status code give the url/api
from urllib.request import urlopen
from urllib.error import URLError, HTTPError
import emoji

#Taking input url from user
requestURL = input("Enter the URL to be invoked: ")

#Gets the response from URL and prints the status code, corresponding emoji and message accordingly
    response = urlopen(requestURL)
    #In case of success, prints success status code and thumbs_up emoji
    print('Status code : ' + str(response.code) + ' ' + emoji.emojize(':thumbs_up:'))
    print('Message : ' + 'Request succeeded. Request returned message - ' + response.reason)
except HTTPError as e:
    #In case of request failure, prints HTTP error status code and thumbs_down emoji
    print('Status : ' + str(e.code) + ' ' + emoji.emojize(':thumbs_down:'))
    print('Message : Request failed. Request returned reason - ' + e.reason)
except URLError as e:
    #In case of bad URL or connection failure, prints Win Error and thumbs_down emoji
    print('Status :',  str(e.reason).split(']')[0].replace('[','') +  ' ' + emoji.emojize(':thumbs_down:'))
    print('Message : '+ str(e.reason).split(']')[1])

Leave a Comment