MCPcopy Create free account
hub / github.com/InternScience/SciReason / check_network_connectivity

Function check_network_connectivity

opencompass/utils/network.py:43–116  ·  view source on GitHub ↗

Check network connectivity using multiple methods with optional proxy support. Args: host: str, target host to check (default: Google DNS "8.8.8.8") port: int, target port to check (default: 53 for DNS) timeout: float, timeout in seconds (default: 3) proxies:

(
        host: str = '8.8.8.8',
        port: int = 53,
        timeout: float = 3,
        proxies: Optional[Dict[str, str]] = None)

Source from the content-addressed store, hash-verified

41
42
43def check_network_connectivity(
44 host: str = '8.8.8.8',
45 port: int = 53,
46 timeout: float = 3,
47 proxies: Optional[Dict[str, str]] = None) -> Tuple[bool, str]:
48 """Check network connectivity using multiple methods with optional proxy
49 support.
50
51 Args:
52 host: str, target host to check (default: Google DNS "8.8.8.8")
53 port: int, target port to check (default: 53 for DNS)
54 timeout: float, timeout in seconds (default: 3)
55 proxies: Optional[Dict[str, str]], proxy configuration (default: None)
56 Example: {
57 'http': 'http://proxy:8080',
58 'https': 'https://proxy:8080'
59 }
60
61 Returns:
62 Tuple[bool, str]: (is_connected, message)
63 """
64
65 # Method 1: Socket connection test (direct connection, no proxy)
66 def check_socket() -> bool:
67 try:
68 socket.create_connection((host, port), timeout=timeout)
69 return True
70 except OSError:
71 return False
72
73 # Method 2: HTTP request test (supports proxy)
74 def check_http() -> bool:
75 try:
76 response = requests.get('http://www.google.com',
77 timeout=timeout,
78 proxies=proxies)
79 return response.status_code == 200
80 except requests.RequestException:
81 return False
82
83 # Method 3: Ping test (direct connection, no proxy)
84 def check_ping() -> bool:
85 param = '-n' if platform.system().lower() == 'windows' else '-c'
86 command = ['ping', param, '1', host]
87 try:
88 return subprocess.call(command,
89 stdout=subprocess.DEVNULL,
90 stderr=subprocess.DEVNULL) == 0
91 except subprocess.SubprocessError:
92 return False
93
94 # Try all methods
95 is_socket_connected = check_socket()
96 is_http_connected = check_http()
97 is_ping_connected = check_ping()
98
99 # Generate detailed message including proxy information
100 status_msg = (

Callers

nothing calls this directly

Calls 3

check_socketFunction · 0.85
check_httpFunction · 0.85
check_pingFunction · 0.85

Tested by

no test coverage detected