devops/script/test.py
2025-12-12 11:40:38 +08:00

22 lines
548 B
Python

import requests
def get_location_by_ip(ip):
url = f"http://ip-api.com/json/{ip}?lang=zh-CN"
response = requests.get(url)
data = response.json()
if data['status'] == 'success':
return {
'国家': data['country'],
'省份': data['regionName'],
'城市': data['city'],
'运营商': data['isp'],
'经度': data['lon'],
'纬度': data['lat']
}
else:
return None
# 示例
ip = '8.8.8.8'
location = get_location_by_ip(ip)
print(location)