主要是调用了别人的接口
# coding=utf-8
import requests
def get_ip_address(ip=''):
url = "http://ip-api.com/json/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
"content-type": "application/json"
}
try:
params = {"lang": "zh-CN"}
resp = requests.get(url + ip, headers=headers, params=params)
resp.raise_for_status() # 检查响应状态码是否正常
data = resp.json() # 解析响应内容为 JSON 格式
print(resp.status_code)
print(data)
except requests.exceptions.RequestException as e:
print("请求发生异常:", e)
if __name__ == "__main__":
get_ip_address('24.48.0.1')
{
'status': 'success',
'country': '加拿大',
'countryCode': 'CA',
'region': 'QC',
'regionName': 'Quebec',
'city': '蒙特利尔',
'zip': 'H1K',
'lat': 45.6085,
'lon': -73.5493,
'timezone': 'America/Toronto',
'isp': 'Le Groupe Videotron Ltee',
'org': 'Videotron Ltee',
'as': 'AS5769 Videotron Telecom Ltee',
'query': '24.48.0.1'
}