-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHTTPStatusCodes.py
More file actions
72 lines (58 loc) · 2.29 KB
/
Copy pathHTTPStatusCodes.py
File metadata and controls
72 lines (58 loc) · 2.29 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
"""HTTPStatusCodes.py
Dictionary of HTTP status codes.
"""
from http import HTTPStatus
__all__ = [
'HTTPStatusCodeList', 'HTTPStatusCodes', 'htmlTableOfHTTPStatusCodes']
# pylint: disable=not-an-iterable
HTTPStatusCodeList = [
(code.value, code.name, code.description) for code in HTTPStatus]
HTTPStatusCodeListColumnNames = ('Code', 'Identifier', 'Description')
# The HTTPStatusCodes can be indexed by either their status code number or
# by a textual identifier. The result is a dictionary with keys code,
# identifier, and description.
HTTPStatusCodes = {}
# Construct HTTPStatusCodes dictionary
for code, identifier, description in HTTPStatusCodeList:
d = {'code': code, 'identifier': identifier, 'description': description,
# the following two exist for backward compatibility only:
'htmlMsg': description, 'asciiMsg': description}
HTTPStatusCodes[code] = d
HTTPStatusCodes[identifier] = d
def htmlTableOfHTTPStatusCodes(
codes=None,
tableArgs='', rowArgs='style="vertical-align:top"',
colArgs='', headingArgs=''):
"""Return an HTML table with HTTP status codes.
Returns an HTML string containing all the status code information
as provided by this module. It's highly recommended that if you
pass arguments to this function, that you do so by keyword.
"""
if codes is None:
codes = HTTPStatusCodeList
tableArgs = ' ' + tableArgs.lstrip() if tableArgs else ''
rowArgs = ' ' + rowArgs.lstrip() if rowArgs else ''
headingArgs = ' ' + headingArgs.lstrip() if headingArgs else ''
colArgs = ' ' + colArgs.lstrip() if colArgs else ''
res = [f'<table{tableArgs}>', '<tr>']
res.extend(f'<th{headingArgs}>{heading}</th>'
for heading in HTTPStatusCodeListColumnNames)
res.append('</tr>')
for code, identifier, description in codes:
res.append(
f'<tr{rowArgs}><td{colArgs}>{code}</td>'
f'<td{colArgs}>{identifier}</td><td{colArgs}>{description}</td>'
'</tr>')
res.append('</table>')
return '\n'.join(res)
# Old (deprecated) alias
HTMLTableOfHTTPStatusCodes = htmlTableOfHTTPStatusCodes
if __name__ == '__main__':
print(f'''<html>
<head>
<title>HTTP Status Codes</title>
</head>
<body>
{htmlTableOfHTTPStatusCodes()}
</body>
</html>''')