Skip to content

Instantly share code, notes, and snippets.

@odysseus0
Created June 20, 2025 09:51
Show Gist options
  • Save odysseus0/ebe21bed2b87fac43414b11af164b3e4 to your computer and use it in GitHub Desktop.
Save odysseus0/ebe21bed2b87fac43414b11af164b3e4 to your computer and use it in GitHub Desktop.
Environment checker - System IPv6 capability analysis
#!/usr/bin/env python3
"""
Check why httpx AsyncClient works on some machines but not others
This will help identify environment differences
"""
import socket
import subprocess
import platform
import asyncio
import httpx
def check_ipv6_connectivity():
"""Check if the system has IPv6 connectivity"""
print("🌐 Checking IPv6 Connectivity:")
print("=" * 50)
# Check if IPv6 is enabled
try:
# Try to create an IPv6 socket
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.close()
print("βœ… IPv6 socket support: Enabled")
except:
print("❌ IPv6 socket support: Disabled")
return False
# Check IPv6 addresses on interfaces
try:
result = subprocess.run(["ifconfig"], capture_output=True, text=True)
ipv6_count = result.stdout.count("inet6")
print(f"πŸ“Š IPv6 addresses found: {ipv6_count}")
# Look for global IPv6 addresses (not link-local)
lines = result.stdout.split('\n')
global_ipv6 = False
for line in lines:
if 'inet6' in line and not 'fe80:' in line and not '::1' in line:
print(f" Global IPv6: {line.strip()}")
global_ipv6 = True
if not global_ipv6:
print(" No global IPv6 addresses (only link-local)")
except:
print(" Could not check network interfaces")
return True
def check_dns_resolution():
"""Check how DNS resolves for TikAPI"""
print("\nπŸ” DNS Resolution Check:")
print("=" * 50)
hostname = "api.tikapi.io"
# Get all addresses
try:
addr_info = socket.getaddrinfo(hostname, 443, socket.AF_UNSPEC, socket.SOCK_STREAM)
ipv4_addrs = []
ipv6_addrs = []
for family, _, _, _, sockaddr in addr_info:
ip = sockaddr[0]
if family == socket.AF_INET:
if ip not in ipv4_addrs:
ipv4_addrs.append(ip)
elif family == socket.AF_INET6:
if ip not in ipv6_addrs:
ipv6_addrs.append(ip)
print(f"βœ… IPv4 addresses: {ipv4_addrs}")
print(f"{'βœ…' if ipv6_addrs else '❌'} IPv6 addresses: {ipv6_addrs if ipv6_addrs else 'None'}")
# Check which one is returned first
first_addr = socket.getaddrinfo(hostname, 443)[0]
first_family = "IPv6" if first_addr[0] == socket.AF_INET6 else "IPv4"
print(f"\n🎯 First address returned: {first_family}")
except Exception as e:
print(f"❌ DNS resolution failed: {e}")
def check_system_info():
"""Check system information"""
print("\nπŸ’» System Information:")
print("=" * 50)
print(f"Platform: {platform.system()} {platform.release()}")
print(f"Python: {platform.python_version()}")
print(f"Machine: {platform.machine()}")
print(f"Processor: {platform.processor()}")
async def test_httpx_behavior():
"""Test actual httpx behavior"""
print("\nπŸ§ͺ Testing httpx AsyncClient:")
print("=" * 50)
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(
"https://api.tikapi.io/public/video",
params={"id": "7003402629929913605"},
headers={
"X-API-KEY": "YOUR_TIKAPI_KEY_HERE",
"Accept": "application/json"
}
)
response.raise_for_status()
print("βœ… httpx AsyncClient works on this machine!")
except httpx.ConnectError as e:
print(f"❌ httpx AsyncClient fails: ConnectError")
print(f" Error details: {repr(e)}")
except Exception as e:
print(f"❌ httpx AsyncClient fails: {type(e).__name__}")
print(f" Error: {e}")
def suggest_fix():
"""Suggest fix based on findings"""
print("\nπŸ’‘ Diagnosis:")
print("=" * 50)
print("If httpx works on some machines but not others, it's likely due to:")
print("1. IPv6 connectivity differences")
print("2. DNS configuration (IPv6 addresses returned)")
print("3. Network environment (home vs corporate)")
print("4. OS/platform differences")
print("\nThe universal fix is to force IPv4-only connections!")
async def main():
print("πŸ” Environment Diagnostic for httpx AsyncClient Issue")
print("=" * 60)
print("This will help explain why it 'works on my machine'\n")
check_system_info()
ipv6_enabled = check_ipv6_connectivity()
check_dns_resolution()
await test_httpx_behavior()
suggest_fix()
print("\nπŸ“‹ Share this output with your colleague to compare!")
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment