httpoll/httpoll.py

67 lines
1.3 KiB
Python
Raw Normal View History

2023-08-18 14:43:41 +02:00
#!/usr/bin/python3
import argparse
import requests
import sys
from time import time, sleep
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
no_proxy = {
# no proxy
'no': 'pass',
}
args = {}
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 = no_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('--use-proxy')
parser.add_argument('-p')
args = parser.parse_args()
if args.use_proxy or args.p:
no_proxy = None
run()
except KeyboardInterrupt:
exit()