Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions backend/app/utils/ip_local_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,20 @@ async def get_ip_location(cls, ip: str) -> str | None:
return "内网IP"

try:
# 使用ip-api.com API获取IP归属地信息
async with httpx.AsyncClient(timeout=settings.HTTPX_DEFAULT_TIMEOUT) as client:
# 尝试使用 ip9.com.cn API
async with httpx.AsyncClient(timeout=5) as client:
# 首选:ip9.com.cn API
url = f"https://ip9.com.cn/get?ip={ip}"
response = await cls._make_api_request(client, url)
if response and response.json().get("ret") == 200:
result = response.json().get("data", {})
return f"{result.get('country', '')}-{result.get('prov', '')}-{result.get('city', '')}-{result.get('area', '')}-{result.get('isp', '')}"

# 尝试使用百度 API
url = f"https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip}"
# 备用:ip-api.com(免费,无需 Key,45次/分钟)
url = f"http://ip-api.com/json/{ip}?lang=zh-CN"
response = await cls._make_api_request(client, url)
if response and response.json().get("code") == "Success":
data = response.json().get("data", {})
# 修正原代码中的格式错误
return f"{data.get('country', '')}-{data.get('prov', '')}-{data.get('city', '')}-{data.get('district', '')}-{data.get('isp', '')}"
if response and response.json().get("status") == "success":
data = response.json()
return f"{data.get('country', '')}-{data.get('regionName', '')}-{data.get('city', '')}-{data.get('isp', '')}"

except Exception as e:
log.error(f"获取IP归属地失败: {e}")
Expand All @@ -119,14 +117,10 @@ async def _make_api_request(cls, client: httpx.AsyncClient, url: str):
返回:
- Response | None: 响应对象,失败时返回None。
"""
max_retries = 3
for attempt in range(max_retries):
try:
response = await client.get(url, timeout=settings.HTTPX_DEFAULT_TIMEOUT)
if response.status_code == 200:
return response
except Exception as e:
if attempt < max_retries - 1:
continue
log.error(f"API 请求失败: {e}")
try:
response = await client.get(url)
if response.status_code == 200:
return response
except Exception as e:
log.error(f"IP 归属地 API 请求失败: {url} - {e}")
return None