From d37e6fdf0bcee88070285af151797daa316dcfc6 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 8 Sep 2021 21:54:03 -0700 Subject: [PATCH] Add networktools.wait_for_internet. --- voussoirkit/networktools.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/voussoirkit/networktools.py b/voussoirkit/networktools.py index 2b67caf..4118324 100644 --- a/voussoirkit/networktools.py +++ b/voussoirkit/networktools.py @@ -7,11 +7,18 @@ internal / external IP addresses. ''' import requests import socket +import time from voussoirkit import vlogging log = vlogging.getLogger(__name__, 'networktools') +class NetworkToolsException(Exception): + pass + +class NoInternet(NetworkToolsException): + pass + def get_external_ip(): url = 'https://voussoir.net/whatsmyip' response = requests.get(url) @@ -44,3 +51,11 @@ def has_internet(timeout=2): return True except socket.error as exc: return False + +def wait_for_internet(timeout): + started = time.time() + while True: + if time.time() - started >= timeout: + raise NoInternet() + if has_internet(timeout=1): + return