Skip to content

Commit e20e518

Browse files
authored
Merge pull request prateekiiest#119 from molex/Issue-#110
Added more status codes. Included some PEP8 formatting changes as wel…
2 parents 1195763 + ab57c72 commit e20e518

File tree

1 file changed

+93
-22
lines changed

1 file changed

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

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"
2895

96+
# added extra line for PEP8
2997
url = input("Please enter a website, inclusive of 'http://' > ")
30-
print(check_url(url))
98+
# created a variable to store the status code
99+
status = check_url(url)
100+
# call new function to print status
101+
print_message(status)

0 commit comments

Comments
 (0)