Add global constant INTERNET_IP.

master
voussoir 2021-10-10 12:02:09 -07:00
parent 1a375e4964
commit 6edba068e5
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 6 additions and 2 deletions

View File

@ -13,6 +13,10 @@ from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'networktools') log = vlogging.getLogger(__name__, 'networktools')
# This is the IP address we'll use to determine if we have an internet
# connection. Change it if this server ever becomes unavailable.
INTERNET_IP = '8.8.8.8'
class NetworkToolsException(Exception): class NetworkToolsException(Exception):
pass pass
@ -32,7 +36,7 @@ def get_lan_ip():
https://stackoverflow.com/a/166589 https://stackoverflow.com/a/166589
''' '''
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(('8.8.8.8', 80)) sock.connect((INTERNET_IP, 80))
return sock.getsockname()[0] return sock.getsockname()[0]
def get_gateway_ip(): def get_gateway_ip():
@ -47,7 +51,7 @@ def has_internet(timeout=2):
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)
sock.connect(('8.8.8.8', 53)) sock.connect((INTERNET_IP, 53))
return True return True
except socket.error as exc: except socket.error as exc:
return False return False