#!/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()