httpoll/httpoll.py

56 lines
1.2 KiB
Python
Raw Normal View History

2023-08-18 14:43:41 +02:00
#!/usr/bin/python3
import argparse
import requests
2023-11-15 19:05:04 +01:00
import urllib3
2023-08-18 14:43:41 +02:00
from time import time, sleep
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
2023-11-15 19:05:04 +01:00
args = dict()
proxy = None
2023-08-18 14:43:41 +02:00
def poll():
"""Try to HTTP GET the URL."""
time_start = time()
status, result = "", ""
try:
2023-11-15 18:32:12 +01:00
r = requests.get(url=args.URL, timeout=10.0,
2023-11-15 19:05:04 +01:00
verify=False, proxies=proxy)
2023-08-18 14:43:41 +02:00
status = "ok"
result = str(r)
except requests.exceptions.RequestException as e:
status = "error"
result = str(e)
duration = format(time() - time_start, ".3f")
print(f"[time: {duration}] [{status}]\t {result}")
def run():
"""Runs endlessly but at most once per second."""
duration = 1
while True:
if duration >= 1:
time_start = time()
poll()
else:
sleep(1 - duration)
duration = time() - time_start
if __name__ == "__main__":
try:
parser = argparse.ArgumentParser()
parser.add_argument('URL')
2023-11-15 19:05:04 +01:00
parser.add_argument('--no-proxy', action="store_true")
2023-08-18 14:43:41 +02:00
args = parser.parse_args()
2023-11-15 19:05:04 +01:00
if args.no_proxy:
proxy = {'no': 'pass'}
2023-08-18 14:43:41 +02:00
run()
except KeyboardInterrupt:
exit()