Skip to content

Commit cd3b415

Browse files
authored
Merge pull request Python-World#227 from AkshataJ96/fetchHttpCode
Added new program for fetching HTTP status code
2 parents 43236eb + f7ef158 commit cd3b415

3 files changed

Lines changed: 47 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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#Program to fetch the http status code give the url/api
2+
from urllib.request import Request, urlopen
3+
from urllib.error import URLError, HTTPError
4+
import emoji
5+
6+
#Taking input url from user
7+
requestURL = input("Enter the URL to be invoked: ")
8+
9+
#Gets the response from URL and prints the status code, corresponding emoji and message accordingly
10+
try:
11+
response = urlopen(requestURL)
12+
#In case of success, prints success status code and thumbs_up emoji
13+
print('Status code : ' + str(response.code) + ' ' + emoji.emojize(':thumbs_up:'))
14+
print('Message : ' + 'Request succeeded. Request returned message - ' + response.reason)
15+
except HTTPError as e:
16+
#In case of request failure, prints HTTP error status code and thumbs_down emoji
17+
print('Status : ' + str(e.code) + ' ' + emoji.emojize(':thumbs_down:'))
18+
print('Message : Request failed. Request returned reason - ' + e.reason)
19+
except URLError as e:
20+
#In case of bad URL or connection failure, prints Win Error and thumbs_down emoji
21+
print('Status :', str(e.reason).split(']')[0].replace('[','') + ' ' + emoji.emojize(':thumbs_down:'))
22+
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)