mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-10-10 03:24:12 +00:00
fix(ip_local_util): 恢复使用百度API获取IP归属地信息
之前切换到了ip-api.com API,现在恢复使用百度API,因为其返回的数据更详细且包含运营商信息
This commit is contained in:
@@ -41,24 +41,24 @@ class IpLocalUtil:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# 百度API失败,使用其他API
|
# 百度API失败,使用其他API
|
||||||
# async with httpx.AsyncClient() as client:
|
|
||||||
# response = await client.get(
|
|
||||||
# f'https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip}',
|
|
||||||
# timeout=5
|
|
||||||
# )
|
|
||||||
# if response.status_code == 200:
|
|
||||||
# data = response.json().get('data', {})
|
|
||||||
# return f"【{data.get('owner','')}】-{data.get('country','')}-{data.get('prov','')}-{data.get('city','')}-{data.get('district','')}"
|
|
||||||
|
|
||||||
# 使用ip-api.com API获取IP归属地信息
|
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
response = await client.get(
|
response = await client.get(
|
||||||
f'http://ip-api.com/json/{ip}?lang=zh-CN',
|
f'https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip}',
|
||||||
timeout=10
|
timeout=10
|
||||||
)
|
)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
result = response.json()
|
data = response.json().get('data', {})
|
||||||
return f"{result.get('country','')}-{result.get('regionName','')}-{result.get('city','')}"
|
return f"【{data.get('owner','')}】-{data.get('country','')}-{data.get('prov','')}-{data.get('city','')}-{data.get('district','')}"
|
||||||
|
|
||||||
|
# 使用ip-api.com API获取IP归属地信息
|
||||||
|
# async with httpx.AsyncClient() as client:
|
||||||
|
# response = await client.get(
|
||||||
|
# f'http://ip-api.com/json/{ip}?lang=zh-CN',
|
||||||
|
# timeout=10
|
||||||
|
# )
|
||||||
|
# if response.status_code == 200:
|
||||||
|
# result = response.json()
|
||||||
|
# return f"{result.get('country','')}-{result.get('regionName','')}-{result.get('city','')}"
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取IP归属地失败: {e}")
|
logger.error(f"获取IP归属地失败: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user