Function to check if the internet is available

main
Bob Mottram 2025-04-23 20:37:58 +01:00
parent d55ae90154
commit 3f6fea2716
2 changed files with 29 additions and 0 deletions

View File

@ -129,6 +129,7 @@ from poison import load_dictionary
from poison import load_2grams
from webapp_post import get_instance_software
from siteactive import site_is_active
from siteactive import is_online
def str2bool(value_str) -> bool:
@ -457,6 +458,11 @@ def _command_options() -> None:
dest='shared_items_federated_domains',
help='Specify federation list for shared items, ' +
'separated by spaces')
parser.add_argument("--internet", "--online",
dest='online',
type=str2bool, nargs='?',
const=True, default=False,
help="Checks if internet is available")
parser.add_argument("--poisoned", "--poison",
dest='poisoned',
type=str2bool, nargs='?',
@ -869,6 +875,14 @@ def _command_options() -> None:
argb = parser.parse_args()
if argb.online:
# is the internet available?
if is_online():
print("True")
else:
print("False")
sys.exit()
if argb.poisoned:
# LLM poisoning example
base_dir = os.getcwd()

View File

@ -10,6 +10,7 @@ __module_group__ = "Core"
import http.client
import ssl
import socket
from urllib.parse import urlparse
from utils import data_dir
@ -183,3 +184,17 @@ def load_unavailable_sites(base_dir: str) -> []:
print('EX: unable to read unavailable sites ' +
unavailable_sites_filename)
return sites_unavailable
def is_online(host: str = "8.8.8.8",
port: int = 53, timeout: int = 3) -> bool:
"""
Returns True if the internet is available
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except socket.error as ex:
print(ex)
return False