Skip to content

Instantly share code, notes, and snippets.

@spookyahell
Last active September 26, 2024 11:50
Show Gist options
  • Save spookyahell/a3143e2535df77c0c11961cd955a7e11 to your computer and use it in GitHub Desktop.
Save spookyahell/a3143e2535df77c0c11961cd955a7e11 to your computer and use it in GitHub Desktop.
[Python] Leveraging Win32 api to get the network state & internet connection state of the machine (via example.com - you can substitute ANYTHING with 100% uptime expectancy]
import ctypes
# Load the Wininet library
wininet = ctypes.windll.wininet
# Define the flags
INTERNET_CONNECTION_MODEM = 0x01
INTERNET_CONNECTION_LAN = 0x02
INTERNET_CONNECTION_PROXY = 0x04
INTERNET_CONNECTION_OFFLINE = 0x20
INTERNET_CONNECTION_CONFIGURED = 0x40
def interpret_flags(flags):
if flags & INTERNET_CONNECTION_MODEM:
return "Modem"
if flags & INTERNET_CONNECTION_LAN:
return "LAN"
if flags & INTERNET_CONNECTION_PROXY:
return "Proxy"
if flags & INTERNET_CONNECTION_OFFLINE:
return "Offline"
if flags & INTERNET_CONNECTION_CONFIGURED:
return "Configured"
def get_connection_state():
flags = ctypes.c_ulong()
result = wininet.InternetGetConnectedState(ctypes.byref(flags), 0)
return result, flags.value
result, flags = get_connection_state()
if result:
print("Connected to the internet")
print("Connection types:", interpret_flags(flags))
else:
print("Not connected to the internet")
print("Connection types:", interpret_flags(flags))
import ctypes
# Load the Wininet library
wininet = ctypes.windll.wininet
# Force Connection Flag, needed to avoid cached states
FLAG_ICC_FORCE_CONNECTION = 0x1​
def check_internet_connection(url="http://example.com"):
result = wininet.InternetCheckConnectionA(url.encode('utf-8'), FLAG_ICC_FORCE_CONNECTION, 0)
return result != 0
if check_internet_connection():
print("Connected to the internet")
else:
print("Not connected to the internet")​
import ctypes
# Load the Wininet library
wininet = ctypes.windll.wininet
# Define the flags
INTERNET_CONNECTION_MODEM = 0x01
INTERNET_CONNECTION_LAN = 0x02
INTERNET_CONNECTION_PROXY = 0x04
INTERNET_CONNECTION_OFFLINE = 0x20
INTERNET_CONNECTION_CONFIGURED = 0x40
def interpret_flags(flags):
states = []
if flags & INTERNET_CONNECTION_MODEM:
states.append("Modem")
if flags & INTERNET_CONNECTION_LAN:
states.append("LAN")
if flags & INTERNET_CONNECTION_PROXY:
states.append("Proxy")
if flags & INTERNET_CONNECTION_OFFLINE:
states.append("Offline")
if flags & INTERNET_CONNECTION_CONFIGURED:
states.append("Configured")
return states
def get_connection_state():
flags = ctypes.c_ulong()
result = wininet.InternetGetConnectedState(ctypes.byref(flags), 0)
return result, flags.value
result, flags = get_connection_state()
if result:
print("Connected to the internet")
print("Connection types:", interpret_flags(flags))
else:
print("Not connected to the internet")
print("Connection types:", interpret_flags(flags))
@spookyahell
Copy link
Author

spookyahell commented Sep 26, 2024

I believe it can only always be ONE of the states, but Microsoft AI wrote the second one all by itself
And that way you're a bit safer for anomalies.

@spookyahell
Copy link
Author

spookyahell commented Sep 26, 2024

Technically i guess it could be LAN and Proxy together, so yeah...
Don't use that first file with the leading underscore in production!

Microsoft Copilot AI confirmed it when asked.
For example, the function might return a combination of INTERNET_CONNECTION_LAN and INTERNET_CONNECTION_PROXY if the system is connected to the internet via a LAN and is also using a proxy server.

Configured State:
The system has network settings configured, such as IP addresses, DNS servers, and gateway information.
(Didn't seem to work during my testing but I was using Wifi so maybe I would have had to re-connect, not sure.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment