Skip to content

Commit

Permalink
Fix is_online() for faster offline imports (#9544)
Browse files Browse the repository at this point in the history
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
  • Loading branch information
3 people committed Apr 6, 2024
1 parent a262865 commit fcb0641
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions ultralytics/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit fcb0641

Please sign in to comment.