-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckResponseCodes.py
More file actions
250 lines (235 loc) · 13.4 KB
/
checkResponseCodes.py
File metadata and controls
250 lines (235 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import requests
import socket
import warnings
from requests.packages.urllib3.exceptions import ConnectionError
from requests.packages.urllib3.exceptions import SSLError as _SSLError
from requests.packages.urllib3.exceptions import TimeoutError
from requests.packages.urllib3.exceptions import HTTPError as _HTTPError
from requests.exceptions import ConnectionError, Timeout, SSLError
# checkResponseCodes is a list of all the http Codes from the
# IANA Hypertext Transfer Protocol (HTTP) Status Code Registry
# in the form of a long 'case statement.'
# I wanted it around so that I could copy ot the subset that I
# needed at any given time.
# It is not meant to be used as is.
"""
# The imports below are a reminder for me when dealing with
# some edge cases.
#import requests.packages.urllib3.exceptions
#from requests.packages.urllib3.response import HTTPResponse
# from requests.exceptions.ConnectionError import
from requests.packages.urllib3.exceptions import ConnectionError
from requests.packages.urllib3.exceptions import MaxRetryError
from requests.packages.urllib3.exceptions import TimeoutError
from requests.packages.urllib3.exceptions import SSLError as _SSLError
from requests.packages.urllib3.exceptions import HTTPError as _HTTPError
from requests.exceptions import ConnectionError, Timeout, SSLError
from requests.packages.urllib3.exceptions import InsecurePlatformWarning
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from requests.packages.urllib3.exceptions import SubjectAltNameWarning
from requests.packages.urllib3.exceptions import SNIMissingWarning
# if you are experimenting in a safe environment, filter some noise,
# otherwise, filtering this stuff is unwise.
warnings.filterwarnings('ignore', category=InsecureRequestWarning)
warnings.filterwarnings('ignore', category=SubjectAltNameWarning)
warnings.filterwarnings('ignore', category=InsecurePlatformWarning)
warnings.filterwarnings('ignore', category=SNIMissingWarning)
"""
# Codes from the IANA Hypertext Transfer Protocol (HTTP) Status Code Registry
# http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
# Last checked 2021-05-05
#
# Categories
# 1xx: Informational - Request received, continuing process
# 2xx: Success - The action was successfully received, understood, and accepted
# 3xx: Redirection - Further action must be taken in order to complete the request
# 4xx: Client Error - The request contains bad syntax or cannot be fulfilled
# 5xx: Server Error - The server failed to fulfill an apparently valid request
#
# RFC2295 https://www.iana.org/go/rfc2295
# RFC2518 https://www.iana.org/go/rfc2518
# RFC2774 https://www.iana.org/go/rfc2774
# RFC3229 https://www.iana.org/go/rfc3229
# RFC4918 https://www.iana.org/go/rfc4918
# RFC5842 https://www.iana.org/go/rfc5842
# RFC6585 https://www.iana.org/go/rfc6585
# RFC7231 https://www.iana.org/go/rfc7231
# RFC7232 https://www.iana.org/go/rfc7232
# RFC7233 https://www.iana.org/go/rfc7233
# RFC7235 https://www.iana.org/go/rfc7235
# RFC7538 https://www.iana.org/go/rfc7238
# RFC7540 https://www.iana.org/go/rfc7540
# RFC7725 https://www.iana.org/go/rfc7725
# RFC8297 https://www.iana.org/go/rfc8297
# RFC8470 https://www.iana.org/go/rfc8470
#
url = 'https://www.google.com'
try:
response = requests.get(url)
if response.status_code == 100:
print(str(response.status_code) + ': Continue') # [RFC7231, Section 6.2.1]
elif response.status_code == 101:
print(str(response.status_code) + ': Switching Protocols') # [RFC7231, Section 6.2.2]
elif response.status_code == 102:
print(str(response.status_code) + ': Processing') # [RFC2518]
elif response.status_code == 103:
print(str(response.status_code) + ': Early Hints') # [RFC8297]
# 104-199 Unassigned
elif response.status_code == 200:
print(str(response.status_code) + ': OK') # [RFC7231, Section 6.3.1]
elif response.status_code == 201:
print(str(response.status_code) + ': Created') # [RFC7231, Section 6.3.2]
elif response.status_code == 202:
print(str(response.status_code) + ': Accepted') # [RFC7231, Section 6.3.3]
elif response.status_code == 203:
print(str(response.status_code) + ': Non-Authoritative Information') # [RFC7231, Section 6.3.4]
elif response.status_code == 204:
print(str(response.status_code) + ': No Content') # [RFC7231, Section 6.3.5]
elif response.status_code == 205:
print(str(response.status_code) + ': Reset Content') # [RFC7231, Section 6.3.6]
elif response.status_code == 206:
print(str(response.status_code) + ': Partial Content') # [RFC7233, Section 4.1]
elif response.status_code == 207:
print(str(response.status_code) + ': Multi-Status') # [RFC4918]
elif response.status_code == 208:
print(str(response.status_code) + ': Already Reported') # [RFC5842]
# 209-225 Unassigned
elif response.status_code == 226:
print(str(response.status_code) + ': IM Used') # [RFC3229]
## 227-299 Unassigned
elif response.status_code == 300:
print(str(response.status_code) + ': Multiple Choices') # [RFC7231, Section 6.4.1]
elif response.status_code == 301:
print(str(response.status_code) + ': Moved Permanently') # [RFC7231, Section 6.4.2]
elif response.status_code == 302:
print(str(response.status_code) + ': Found') # [RFC7231, Section 6.4.3]
elif response.status_code == 303:
print(str(response.status_code) + ': See Other') # [RFC7231, Section 6.4.4]
elif response.status_code == 304:
print(str(response.status_code) + ': Not Modified') # [RFC7232, Section 4.1]
elif response.status_code == 305:
print(str(response.status_code) + ': Use Proxy') # [RFC7231, Section 6.4.5]
elif response.status_code == 306:
print(str(response.status_code) + ': Unused') # [RFC7231, Section 6.4.6]
elif response.status_code == 307:
print(str(response.status_code) + ': Temporary Redirect') # [RFC7231, Section 6.4.7]
elif response.status_code == 308:
print(str(response.status_code) + ': Permanent Redirect') # [RFC7538]
# 309-399 Unassigned
elif response.status_code == 400:
print(str(response.status_code) + ': Bad Request') # [RFC7231, Section 6.5.1]
elif response.status_code == 401:
print(str(response.status_code) + ': Unauthorized') # [RFC7235, Section 3.1]
elif response.status_code == 402:
print(str(response.status_code) + ': Payment Required') # [RFC7231, Section 6.5.2]
elif response.status_code == 403:
print(str(response.status_code) + ': Forbidden') # [RFC7231, Section 6.5.3]
elif response.status_code == 404:
print(str(response.status_code) + ': Not Found') # [RFC7231, Section 6.5.4]
elif response.status_code == 405:
print(str(response.status_code) + ': Method Not Allowed') # [RFC7231, Section 6.5.5]
elif response.status_code == 406:
print(str(response.status_code) + ': Not Acceptable') # [RFC7231, Section 6.5.6]
elif response.status_code == 407:
print(str(response.status_code) + ': Proxy Authentication Required') # [RFC7235, Section 3.2]
elif response.status_code == 408:
print(str(response.status_code) + ': Request Timeout') # [RFC7231, Section 6.5.7]
elif response.status_code == 409:
print(str(response.status_code) + ': Conflict') # [RFC7231, Section 6.5.8]
elif response.status_code == 410:
print(str(response.status_code) + ': Gone') # [RFC7231, Section 6.5.9]
elif response.status_code == 411:
print(str(response.status_code) + ': Length Required') # [RFC7231, Section 6.5.10]
elif response.status_code == 412:
print(str(response.status_code) + ': Precondition Failed') # [RFC7232, Section 4.2][RFC8144, Section 3.2]
elif response.status_code == 413:
print(str(response.status_code) + ': Payload Too Large') # [RFC7231, Section 6.5.11]
elif response.status_code == 414:
print(str(response.status_code) + ': URI Too Long') # [RFC7231, Section 6.5.12]
elif response.status_code == 415:
print(str(response.status_code) + ': Unsupported Media Type') # [RFC7231, Section 6.5.13][RFC7694, Section 3]
elif response.status_code == 416:
print(str(response.status_code) + ': Range Not Satisfiable') # [RFC7233, Section 4.4]
elif response.status_code == 417:
print(str(response.status_code) + ': Expectation Failed') # [RFC7231, Section 6.5.14]
# 418-420 Unassigned
elif response.status_code == 421:
print(str(response.status_code) + ': Misdirected Request') # [RFC7540, Section 9.1.2]
elif response.status_code == 422:
print(str(response.status_code) + ': Unprocessable Entity') # [RFC4918]
elif response.status_code == 423:
print(str(response.status_code) + ': Locked') # [RFC4918]
elif response.status_code == 424:
print(str(response.status_code) + ': Failed Dependency') # [RFC4918]
elif response.status_code == 425:
print(str(response.status_code) + ': Too Early') # [RFC8470]
elif response.status_code == 426:
print(str(response.status_code) + ': Upgrade Required') # [RFC7231, Section 6.5.15]
elif response.status_code == 427:
print(str(response.status_code) + ': Unassigned')
elif response.status_code == 428:
print(str(response.status_code) + ': Precondition Required') # [RFC6585]
elif response.status_code == 429:
print(str(response.status_code) + ': Too Many Requests') # [RFC6585]
elif response.status_code == 430:
print(str(response.status_code) + ': Unassigned')
elif response.status_code == 431:
print(str(response.status_code) + ': Request Header Fields Too Large') [RFC6585]
# 432-450 Unassigned
elif response.status_code == 451:
print(str(response.status_code) + ': Unavailable For Legal Reasons') # [RFC7725]
# 452-499 Unassigned
elif response.status_code == 500:
print(str(response.status_code) + ': Internal Server Error') # [RFC7231, Section 6.6.1]
elif response.status_code == 501:
print(str(response.status_code) + ': Not Implemented') # [RFC7231, Section 6.6.2]
elif response.status_code == 502:
print(str(response.status_code) + ': Bad Gateway') # [RFC7231, Section 6.6.3]
elif response.status_code == 503:
print(str(response.status_code) + ': Service Unavailable') # [RFC7231, Section 6.6.4]
elif response.status_code == 504:
print(str(response.status_code) + ': Gateway Timeout') # [RFC7231, Section 6.6.5]
elif response.status_code == 505:
print(str(response.status_code) + ': HTTP Version Not Supported') # [RFC7231, Section 6.6.6]
elif response.status_code == 506:
print(str(response.status_code) + ': Variant Also Negotiates') # [RFC2295]
elif response.status_code == 507:
print(str(response.status_code) + ': Insufficient Storage') # [RFC4918]
elif response.status_code == 508:
print(str(response.status_code) + ': Loop Detected') # [RFC5842]
elif response.status_code == 509:
print(str(response.status_code) + ': Unassigned')
elif response.status_code == 510:
print(str(response.status_code) + ': Not Extended') # [RFC2774]
elif response.status_code == 511:
print(str(response.status_code) + ': Network Authentication Required') # [RFC6585]
# 512-599 Unassigned
elif response.status_code:
print(str(response.status_code) + ': Unknown response, there is a problem!')
else:
print('Unknown condition, there is a problem!')
except OSError as msg:
print('---------------------------------------------------------------')
print('Reached OSError -----------------------------------------------')
print('Something bad happened. Hostname correct? Network OK?')
print('---------------------------------------------------------------')
#raise OSError()
except socket.error as sockerr:
print('---------------------------------------------------------------')
print('socket.error --------------------------------------------------')
print('Something bad happened. Hostname correct? Network OK?')
print('---------------------------------------------------------------')
raise ConnectionError(sockerr)
except ConnectionError as e:
raise ConnectionError(e)
except MaxRetryError as e:
raise ConnectionError(e)
except ConnectionError as e:
raise ConnectionError(e)
except (_SSLError, _HTTPError) as e:
if isinstance(e, _SSLError):
raise SSLError(e)
elif isinstance(e, TimeoutError):
raise Timeout(e)
else:
raise Timeout('Request timed out.')