使用Python语言查询IP地址
import requests
def get_ip_location(ip):
url = f"http://ip-api.com/json/{ip}?lang=zh-CN"
try:
response = requests.get(url, timeout=5)
data = response.json()
if data['status'] == 'success':
return {
'country': data.get('country', ''),
'region': data.get('regionName', ''),
'city': data.get('city', ''),
'isp': data.get('isp', '')
}
else:
return "查询失败"
except Exception as e:
return f"错误: {e}"
print(get_ip_location("ip_address"))
程序输出
{'country': '美国', 'region': '弗吉尼亚州', 'city': 'Ashburn', 'isp': 'Google LLC'}