Skip to content

Commit

Permalink
add multiple fallbacks in check_internet_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
amit9838 committed Feb 13, 2024
1 parent d93743f commit 3e21a86
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,44 @@
epoch_offset = None


def check_internet_connection():
has_active_internet = False
TIMEOUT = 5
domains = {
"google": "http://www.google.com",
"wikipedia": "https://www.wikipedia.org/",
"baidu": "https://www.baidu.com/", # Specifically for china
}

# Check internet connection using socket connecton
def check_internet_socket():
try:
socket.create_connection(("1.1.1.1", 53), timeout=5) # 53 is the DNS port
has_active_internet = True
return has_active_internet
socket.create_connection(("1.1.1.1", 53), timeout=TIMEOUT) # 53 is the DNS port
print("Internet conncetion confirmed through socket connection")
return True
except OSError:
return has_active_internet
return False


# Check Internet connection using requests
def check_internet_domain(url):
try:
request = requests.get(url, timeout=TIMEOUT)
print("Internet conncetion confirmed through: ",url)
return True
except (requests.ConnectionError, requests.Timeout) as exception:
return False


def check_internet_connection():
if (
check_internet_socket()
or check_internet_domain(domains["google"])
or check_internet_domain(domains["wikipedia"])
or check_internet_domain(domains["baidu"])
):
return True

print("No internet!")
return False


def get_selected_city_coords():
Expand Down

0 comments on commit 3e21a86

Please sign in to comment.