diff --git a/backend/app/utils/ip_local_util.py b/backend/app/utils/ip_local_util.py index 9c1c8afd..c8dff4ea 100644 --- a/backend/app/utils/ip_local_util.py +++ b/backend/app/utils/ip_local_util.py @@ -2,6 +2,7 @@ import re import requests +import httpx from app.core.logger import logger @@ -42,31 +43,25 @@ class IpLocalUtil: return '内网IP' try: - # 优先使用百度API - ip_result = requests.get( - f'https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip}', - timeout=5 - ) - result = ip_result.json() - if result['code']== 'Success': - - data = result['data'] - logger.info(f"获取IP归属地成功: {data}") - # "continent": "亚洲", - # "country": "中国", - # "zipcode": "710061", - # "owner": "中国移动", - # "isp": "中国移动", - # "adcode": "610113", - # "prov": "陕西省", - # "city": "西安市", - # "district": "雁塔区" - country = data['country'] - owner = data['owner'] - prov = data['prov'] - city = data['city'] - district = data['district'] - return f'【{owner}】-{country}-{prov}-{city}-{district}' + # 百度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: + # 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('isp','')}】-{result.get('country','')}-{result.get('regionName','')}-{result.get('city','')}" except Exception as e: logger.error(f"获取IP归属地失败: {e}") diff --git a/backend/requirements.txt b/backend/requirements.txt index f58dbaf0..49e0aaa6 100755 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -6,6 +6,7 @@ uvicorn==0.30.6 # uvicorn web 框架 websockets==14.2 # websocket 框架 gunicorn==23.0.0 # 协程框架 # celery==5.4.0 # 任务队列(移除,使用APScheduler) +httpx==0.28.1 # HTTP 客户端 croniter==6.0.0 # 实现cron表达式验证和解析执行计划 pandas==2.2.2 # 数据处理 openpyxl==3.1.5 # Excel