Add some docstrings, return annotations.
This commit is contained in:
parent
6edba068e5
commit
a94de2adde
1 changed files with 16 additions and 4 deletions
|
@ -23,14 +23,14 @@ class NetworkToolsException(Exception):
|
||||||
class NoInternet(NetworkToolsException):
|
class NoInternet(NetworkToolsException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_external_ip():
|
def get_external_ip() -> str:
|
||||||
url = 'https://voussoir.net/whatsmyip'
|
url = 'https://voussoir.net/whatsmyip'
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
ip = response.text.strip()
|
ip = response.text.strip()
|
||||||
return ip
|
return ip
|
||||||
|
|
||||||
def get_lan_ip():
|
def get_lan_ip() -> str:
|
||||||
'''
|
'''
|
||||||
thank you unknwntech
|
thank you unknwntech
|
||||||
https://stackoverflow.com/a/166589
|
https://stackoverflow.com/a/166589
|
||||||
|
@ -47,7 +47,10 @@ def has_lan():
|
||||||
# Open a socket to the router
|
# Open a socket to the router
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def has_internet(timeout=2):
|
def has_internet(timeout=2) -> bool:
|
||||||
|
'''
|
||||||
|
Return True if an internet connection is available.
|
||||||
|
'''
|
||||||
socket.setdefaulttimeout(timeout)
|
socket.setdefaulttimeout(timeout)
|
||||||
try:
|
try:
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
@ -56,7 +59,16 @@ def has_internet(timeout=2):
|
||||||
except socket.error as exc:
|
except socket.error as exc:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def wait_for_internet(timeout):
|
def wait_for_internet(timeout) -> None:
|
||||||
|
'''
|
||||||
|
This function blocks until an internet connection is available, or the
|
||||||
|
timeout is reached.
|
||||||
|
|
||||||
|
Raises NoInternet if the timeout expires.
|
||||||
|
'''
|
||||||
|
if timeout <= 0:
|
||||||
|
raise ValueError(f'timeout should be greater than 0, not {timeout}.')
|
||||||
|
|
||||||
started = time.time()
|
started = time.time()
|
||||||
while True:
|
while True:
|
||||||
if time.time() - started >= timeout:
|
if time.time() - started >= timeout:
|
||||||
|
|
Loading…
Reference in a new issue