-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_status_check.py
126 lines (107 loc) · 3.84 KB
/
http_status_check.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "Steve Lenz"
__copyright__ = "Copyright 2020"
__license__ = "MIT"
__version__ = "0.1.0"
__maintainer__ = "Steve Lenz"
__email__ = "kontakt@steve-lenz"
import sys
from utility import output
# Check Python version
assert sys.version_info >= (3, 7), f'{output.Subject.ERROR} This script needs minimum Python 3.7!'
# Check requirements
try:
import argparse
import yaml
import requests
except ImportError:
print(
f'{output.Subject.ERROR} Missing requirements! Install requirements with pip: pip3 install -r requirements.txt')
sys.exit(1)
class HTTPStatusCheck:
"""
Check multiple URL routes against an expected HTTP status code.
"""
args = []
configuration = []
def __init__(self):
"""
Initialization
:return:
"""
self.print_info()
self.get_arguments()
self.load_configuration()
self.run_checks()
print(f'{output.Subject.OK} Done.')
def load_configuration(self):
"""
Loads data from YAML configuration
:return:
"""
print(f'{output.Subject.INFO} Read configuration from {self.args.config}')
self.configuration = yaml.safe_load(open(self.args.config))
self.print_debug(self.configuration)
def run_checks(self):
"""
Runs access verification
:return:
"""
print(f'{output.Subject.INFO} Check routes against expected status code: {output.CliTextColor.GREEN}{self.configuration["expectedHttpStatusCode"]}{output.CliTextColor.ENDC}')
for file in self.configuration['routes']:
url = f'{self.configuration["domain"]}{file}'
self.print_debug(f'Request {url}')
try:
request = requests.get(url)
except requests.exceptions.RequestException as e:
print(f'{output.Subject.ERROR}')
raise SystemExit(e)
self.print_debug(f'Response headers: {request.headers}')
if self.configuration['expectedHttpStatusCode'] == request.status_code:
print(f'{output.Subject.OK} {url} returns HTTP status {output.CliTextColor.GREEN}{request.status_code}{output.CliTextColor.ENDC}')
else:
print(f'{output.Subject.ERROR} {url} returns HTTP status {output.CliTextColor.RED}{request.status_code}{output.CliTextColor.ENDC}')
def get_arguments(self):
"""
Parses and returns script arguments
:param self:
:return:
"""
parser = argparse.ArgumentParser()
parser.add_argument('--config',
help='Absolute path to config.yaml',
required=True,
type=str)
parser.add_argument('--debug',
help='Enable debug output',
required=False,
action='store_true')
self.args = parser.parse_args()
def print_debug(self, content):
"""
Prints debug output
:param self:
:param content:
:return:
"""
if self.args.debug:
print(f'{output.Subject.DEBUG} {content}')
def print_info(self):
"""
Prints app information
:return:
"""
print(output.CliTextColor.BLUE)
print('*****************************************')
print('* *')
print('* HTTP Status Check *')
print(f'* {output.CliTextColor.ENDC}by {__author__}{output.CliTextColor.BLUE} *')
print('* *')
print('*****************************************')
print(output.CliTextColor.ENDC)
#
# MAIN
#
if __name__ == "__main__":
HTTPStatusCheck()