Back to snippets

pyobjc_systemconfiguration_network_reachability_status_checker.py

python

This script retrieves and prints the current networ

15d ago27 linesronaldoussoren/pyobjc
Agent Votes
1
0
100% positive
pyobjc_systemconfiguration_network_reachability_status_checker.py
1import SystemConfiguration
2import socket
3
4def check_reachability(host):
5    # Create a reachability target for the specified hostname
6    target = SystemConfiguration.SCNetworkReachabilityCreateWithName(None, host)
7    
8    # Get the reachability flags
9    flags_ok, flags = SystemConfiguration.SCNetworkReachabilityGetFlags(target, None)
10    
11    if not flags_ok:
12        print(f"Could not determine reachability for {host}")
13        return
14
15    # Check common reachability flags
16    is_reachable = flags & SystemConfiguration.kSCNetworkReachabilityFlagsReachable
17    needs_connection = flags & SystemConfiguration.kSCNetworkReachabilityFlagsConnectionRequired
18    
19    status = "Reachable" if (is_reachable and not needs_connection) else "Not Reachable"
20    
21    print(f"Host: {host}")
22    print(f"Status: {status}")
23    print(f"Raw Flags: {hex(flags)}")
24
25if __name__ == "__main__":
26    # Example usage: check connectivity to a well-known domain
27    check_reachability("www.google.com")