Skip to content

Commit 1d2e315

Browse files
committed
Added more status codes. Included some PEP8 formatting changes as well. I also moved code into functions to better encapsulate functionality. Also moved if/elif/else to a dictionary to be more concise and readable.
1 parent 46a40b4 commit 1d2e315

File tree

1 file changed

+79
-22
lines changed

1 file changed

+79
-22
lines changed
Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,87 @@
11
import requests
22

3-
# TODO: Add more status code functionality.
4-
53

64
def check_url(url):
75
# This function uses status codes to check various states of any website.
6+
# modified to only return the status code
7+
try:
8+
r = requests.get(url) # added spacing for PEP8
9+
return r.status_code
10+
except requests.ConnectionError:
11+
return 999 # added default code in case of failure in the requests library
12+
13+
14+
def print_message(code):
15+
statusMessage = {
16+
100: 'Website is slow to respond, but appears ok.',
17+
101: 'Server is upgrading to the requested protocol.',
18+
102: 'Request received, website has yet to respond.',
19+
200: "Website is online.",
20+
201: "The request has succeeded and a new resource has been created.",
21+
202: "The request has been received but not yet acted upon.",
22+
203: "Website returned meta-information from a copy of the origin server.",
23+
204: "There is no content to send for this request.",
24+
205: "Reset document view which sent this request.",
25+
206: "Separate download into multiple streams.",
26+
207: "Website returned XML which could contain multiple status codes.",
27+
208: "Website issued multiple responses.",
28+
226: "Website returned a GET response.",
29+
300: "Website has different choices and cannot be resolved into one.",
30+
301: "Website has been redirected permanently.",
31+
302: "Website has been redirected temporarily.",
32+
303: "Website sent this response to direct the client to get the requested resource at another URI.",
33+
304: "Website cache has not been modified.",
34+
305: "Website requested response must be accessed by a proxy.",
35+
306: "Unused staus code, held for upcoming purpose",
36+
307: "Website has been redirected temporarily.",
37+
308: "Website has been redirected permanently.",
38+
400: "The request could not be understood by the server due to malformed syntax.",
39+
401: "The request requires user authentication",
40+
402: "Website requires payment before serving responses.",
41+
403: "Forbidden. The server understood the request, but is refusing to fulfill it.",
42+
404: "Website not found!",
43+
405: "The request method is known by the server but has been disabled and cannot be used.",
44+
406: "No Content found!",
45+
407: "Website requires 3rd party authentication.",
46+
408: "Website request Timed out.",
47+
409: "Server conflict, please try again.",
48+
410: "Requested content has been permanently deleted from server.",
49+
411: 'Website rejected the request because the Content-Length header field is not defined.',
50+
412: "Website doesn't meet client preconditions.",
51+
413: "Request entity is larger than limits defined by server.",
52+
414: "The URI requested by the client is too long.",
53+
415: "The media format of the requested data is not supported by the server.",
54+
416: "The range specified by the Range header field in the request can't be fulfilled by the website.",
55+
417: "Data indicated by the Expect request header field can't be met by the server.",
56+
418: "The server refuses the attempt to brew coffee with a teapot.",
57+
421: "Website redirection failed.",
58+
422: "The request failed due to semantic errors.",
59+
423: "The resource that is being accessed is locked.",
60+
424: "The request failed due to failure of a previous request.",
61+
426: "The server refuses to perform the request using the current protocol.",
62+
428: "Data update conflict.",
63+
429: "Website refused due to too many requests, please try again later.",
64+
431: "Request refused due to large size headers.",
65+
451: "Request refused due to legal reasons.",
66+
500: "Website is experiencing errors.",
67+
501: "Unsupported request.",
68+
502: "Website Gateway error.",
69+
503: "The web server is unable to handle your HTTP request at the time.",
70+
504: "Gateway Timeout.",
71+
505: "HTTP Version Not Supported.",
72+
506: "The server has an internal configuration error.",
73+
507: "The server is out of space.",
74+
508: "The server went to infinity and beyond, but could not return your request.",
75+
510: "Further extensions to the request are required for the server to fulfill it.",
76+
511: "Network Authentication Required.",
77+
999: 'Failed to connect.'
78+
}
79+
print(statusMessage[status])
880

9-
r=requests.get(url)
10-
if r.status_code == 200:
11-
return "Website is online."
12-
elif r.status_code == 300:
13-
return "Website has different choices and cannot be resolved into one"
14-
elif r.status_code == 301:
15-
return "Website has been redirected permanently."
16-
elif r.status_code == 302:
17-
return "Website has been redirected temporarily"
18-
elif r.status_code == 400:
19-
return "The request could not be understood by the server due to malformed syntax"
20-
elif r.status_code == 401:
21-
return "The request requires user authentication"
22-
elif r.status_code == 403:
23-
return "Forbidden. The server understood the request, but is refusing to fulfill it"
24-
elif r.status_code == 404:
25-
return "Website not found!"
26-
elif r.status_code == 503:
27-
return "The web server is unable to handle your HTTP request at the time"
2881

82+
# added extra line for PEP8
2983
url = input("Please enter a website, inclusive of 'http://' > ")
30-
print(check_url(url))
84+
# created a variable to store the status code
85+
status = check_url(url)
86+
# call new function to print status
87+
print_message(status)

0 commit comments

Comments
 (0)