import socket
import requests
import urllib3.util.connection as urllib3_connection
def get_json(url, family=None):
original_family = urllib3_connection.allowed_gai_family
session = requests.Session()
session.trust_env = False
if family is not None:
urllib3_connection.allowed_gai_family = lambda: family
try:
response = session.get(url, timeout=10)
response.raise_for_status()
return response.json()
finally:
session.close()
urllib3_connection.allowed_gai_family = original_family
def check_network():
"""檢測IPv4/IPv6支援情况"""
# IPv4檢測
print("? IPv4檢測:")
try:
data = get_json('https://v4.17nas.com?format=json', socket.AF_INET)
if 'error' in data:
print(f"❌ {data['error']['zh']}")
else:
print(f"✅ IPv4地址: {data['ip']}")
print(f" 國家: {data['country']['zh']} ({data['country']['code']})")
print(f" 位置: {data['location']['zh']}")
except Exception as e:
print(f"⚠️ 檢測失败: {e}")
# IPv6檢測
print("\n? IPv6檢測:")
try:
data = get_json('https://v6.17nas.com?format=json', socket.AF_INET6)
if 'error' in data:
print(f"❌ {data['error']['zh']}")
else:
print(f"✅ IPv6地址: {data['ip']}")
print(f" 國家: {data['country']['zh']}")
except Exception as e:
print(f"⚠️ 檢測失败: {e}")
# 存取優先級
print("\n⚡ 存取優先級:")
try:
data = get_json('https://ip.17nas.com?format=json')
print(f"協議: {data['protocol']}")
print(f"優先級: {data['priority']['zh']}")
except Exception as e:
print(f"⚠️ 檢測失败: {e}")
if __name__ == '__main__':
check_network()