From fcb0641d7fe960f787fd871e9faa8d70372d1819 Mon Sep 17 00:00:00 2001 From: khoalu <42468826+khoalu@users.noreply.github.com> Date: Sun, 7 Apr 2024 02:33:22 +0700 Subject: [PATCH] Fix `is_online()` for faster offline imports (#9544) Co-authored-by: Glenn Jocher Co-authored-by: UltralyticsAssistant --- ultralytics/utils/__init__.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/ultralytics/utils/__init__.py b/ultralytics/utils/__init__.py index 5863642c..9aaf0fd7 100644 --- a/ultralytics/utils/__init__.py +++ b/ultralytics/utils/__init__.py @@ -475,17 +475,12 @@ def is_online() -> bool: Returns: (bool): True if connection is successful, False otherwise. """ - import socket + with contextlib.suppress(Exception): + assert str(os.getenv("YOLO_OFFLINE", "")).lower() != "true" # check if ENV var YOLO_OFFLINE="True" + import socket - for host in "1.1.1.1", "8.8.8.8", "223.5.5.5": # Cloudflare, Google, AliDNS: - try: - test_connection = socket.create_connection(address=(host, 80), timeout=2) - except (socket.timeout, socket.gaierror, OSError): - continue - else: - # If the connection was successful, close it to avoid a ResourceWarning - test_connection.close() - return True + socket.create_connection(address=("1.1.1.1", 80), timeout=1.0).close() # check Cloudflare DNS + return True return False