Skip to content

Commit 8223542

Browse files
committed
Added new program for fetching HTTP status code
1 parent 6ac0891 commit 8223542

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Program to fetch the HTTP status code
2+
3+
This script is used to fetch the status code of any request.
4+
5+
Input:
6+
-> URL of a website or an API
7+
8+
Output:
9+
-> Line 1 : Status code of the response followed by emoji (thumbsup/thumbsdown)
10+
-> Line 2 : Relevant message about the status code.
11+
i.e. If status code corresponds to failure, then reason of failure would be shown in the message field.
12+
13+
## Prerequisites
14+
15+
This program uses and external dependency of "emoji" library to display the emojis in output.
16+
This library can be installed easily by using the following command:
17+
pip install -r requirements.txt
18+
19+
## How to use this script?
20+
21+
-> Install the requirements.
22+
->Type the following command:
23+
python fetch_http_status_code.py
24+
->A message asking URL/API would be displayed : Enter any url of choice and check the output
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from urllib.request import Request, urlopen
2+
from urllib.error import URLError, HTTPError
3+
import emoji
4+
5+
requestURL = input("Enter the URL to be invoked: ")
6+
7+
try:
8+
response = urlopen(requestURL)
9+
print('Status code : ' + str(response.code) + ' ' + emoji.emojize(':thumbs_up:'))
10+
print('Message : ' + 'Request succeeded. Request returned message - ' + response.reason)
11+
except HTTPError as e:
12+
print('Status : ' + str(e.code) + ' ' + emoji.emojize(':thumbs_down:'))
13+
print('Message : Request failed. Request returned reason - ' + e.reason)
14+
except URLError as e:
15+
print('Status :', str(e.reason).split(']')[0].replace('[','') + ' ' + emoji.emojize(':thumbs_down:'))
16+
print('Message : '+ str(e.reason).split(']')[1])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
emoji==0.6.0

0 commit comments

Comments
 (0)