httpoll/httpoll.py
Marius Alwan Meyer 74650baf5d
All checks were successful
Python Lint & Test / build (push) Successful in 11s
cleaned up the proxy situation
2023-11-15 19:05:04 +01:00

56 lines
1.2 KiB
Python
Executable File

#!/usr/bin/python3
import argparse
import requests
import urllib3
from time import time, sleep
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
args = dict()
proxy = None
def poll():
"""Try to HTTP GET the URL."""
time_start = time()
status, result = "", ""
try:
r = requests.get(url=args.URL, timeout=10.0,
verify=False, proxies=proxy)
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')
parser.add_argument('--no-proxy', action="store_true")
args = parser.parse_args()
if args.no_proxy:
proxy = {'no': 'pass'}
run()
except KeyboardInterrupt:
exit()