#!/usr/bin/python3 import argparse import requests import urllib3 import time from datetime import datetime import os import sys from rich import print urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) args = dict() proxy = { "http": os.environ.get('http_proxy'), "https": os.environ.get('https_proxy') } def poll(url): """Try to HTTP GET the URL.""" status, result = "", "" time_start = time.time() try: r = requests.get(url=url, timeout=50.0, verify=False, proxies=proxy) status = "[bold green] up[/bold green]" result = str(r) except requests.exceptions.RequestException as e: status = "[bold red] down[/bold red]" result = str(e) return { "duration": (time.time() - time_start), "status": status, "result": result } def run(global_time_start): """Runs endlessly but at most once per second.""" while True: time_start = time.time() results = [] for target in args.URL: results.append(poll(target)) global_time = datetime.now().replace(microsecond=0) - global_time_start print(f"time: {global_time} | ", end="") for index, result in enumerate(results): print(f"pa{index}: {result['status']}\t |", end="") print() time_elapsed = time.time() - time_start time_to_sleep = max(1.0 - time_elapsed, 0) time.sleep(time_to_sleep) if __name__ == "__main__": try: parser = argparse.ArgumentParser() parser.add_argument('URL', nargs='*') parser.add_argument('--no-proxy', action="store_true") args = parser.parse_args() if args.no_proxy: proxy = {'no': 'pass'} global_time_start = datetime.now().replace(microsecond=0) run(global_time_start) except KeyboardInterrupt: sys.exit()