Last active
August 18, 2025 14:08
-
-
Save scivision/6d985750f9a238171ef2c141e1c83a4c to your computer and use it in GitHub Desktop.
Python requests to test if captive portal is grabbing connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S uv run --script | |
# /// script | |
# requires-python = ">=3.9" | |
# dependencies = ["requests"] | |
# /// | |
from __future__ import annotations | |
import requests | |
def get_urls() -> dict[str, tuple[str, int]]: | |
urls = { | |
"Firefox": ("http://detectportal.firefox.com/success.txt", 200), | |
"Windows": ("http://www.msftconnecttest.com/connecttest.txt", 200), | |
"macOS": ("http://captive.apple.com/hotspot-detect.html", 200), | |
"Fedora": ("http://fedoraproject.org/static/hotspot.txt", 200), | |
"Google": ("http://connectivitycheck.gstatic.com/generate_204", 204), | |
"Android": ("http://connectivitycheck.android.com/generate_204", 204), | |
"Ubuntu": ("http://connectivity-check.ubuntu.com/generate_204", 204), | |
} | |
return urls | |
def check_connectivity(url: str) -> int | None: | |
try: | |
response = requests.head(url, timeout=5) | |
return response.status_code | |
except requests.RequestException as e: | |
print(f"Error checking {url}: {e}") | |
return None | |
if __name__ == "__main__": | |
urls = get_urls() | |
ok = "\u2705" | |
bad = "\u274c" | |
for platform, (url, expected_status) in urls.items(): | |
status_code = check_connectivity(url) | |
if status_code is None: | |
print(f"{platform}: {url} could not be reached {bad}\n") | |
elif status_code == expected_status: | |
print(f"{platform}: {url} returned {status_code} {ok}\n") | |
else: | |
print( | |
f"{platform}: {url} returned {status_code} (expected {expected_status}) {bad}\n" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment