Add networktools.wait_for_internet.
This commit is contained in:
parent
0274b9d4a5
commit
d37e6fdf0b
1 changed files with 15 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue