fix: connection check failing (#1280)

This commit is contained in:
Lukas Trippe 2024-09-11 16:31:19 +02:00 committed by GitHub
parent 6f39dc7da1
commit 4524aca5d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -115,20 +115,26 @@ def input_custom_extra_functionality(w):
return [] return []
# Check if the workflow has access to the internet by trying to access the HEAD of specified url def has_internet_access(url: str = "https://www.zenodo.org", timeout: int = 3) -> bool:
def has_internet_access(url="www.zenodo.org") -> bool: """
import http.client as http_client Checks if internet connection is available by sending a HEAD request
to a reliable server like Google.
# based on answer and comments from Parameters:
# https://stackoverflow.com/a/29854274/11318472 - url (str): The URL to check for internet connection. Default is Google.
conn = http_client.HTTPConnection(url, timeout=5) # need access to zenodo anyway - timeout (int | float): The maximum time (in seconds) the request should wait.
Returns:
- bool: True if the internet is available, otherwise False.
"""
try: try:
conn.request("HEAD", "/") # Send a HEAD request to avoid fetching full response
return True response = requests.head(url, timeout=timeout, allow_redirects=True)
except: return response.status_code == 200
except requests.ConnectionError: # (e.g., no internet, DNS issues)
return False
except requests.Timeout: # (e.g., slow or no network)
return False return False
finally:
conn.close()
def solved_previous_horizon(w): def solved_previous_horizon(w):