Spaces:
Sleeping
Sleeping
update
Browse files- app.py +2 -7
- proxy copy 2.py +44 -0
- proxy.py +8 -0
app.py
CHANGED
|
@@ -73,17 +73,12 @@ async def proxy_gemini(request: Request, protocol: ProtocolType, host:str, path:
|
|
| 73 |
# 将User-Agent改为curl/8.7.1,以模拟curl请求
|
| 74 |
client_headers["User-Agent"] = "curl/8.7.1"
|
| 75 |
|
| 76 |
-
|
| 77 |
-
api_key_info = await get_api_key_info('gemini-2.5-flash')
|
| 78 |
-
api_key = api_key_info.get('api_key')
|
| 79 |
-
api_key_show = api_key[:5]+'*****'+api_key[-5:]
|
| 80 |
-
print(f"使用API密钥:{api_key_show}")
|
| 81 |
-
client_headers["Authorization"] = f"Bearer {api_key}"
|
| 82 |
# api_key_id = api_key_info.get('api_key_id')
|
| 83 |
|
| 84 |
print('\n\n-----------')
|
| 85 |
print(client_headers)
|
| 86 |
print('-----------\n\n')
|
| 87 |
|
| 88 |
-
# 调用 do_proxy
|
| 89 |
return await do_proxy(real_url, request.method, client_headers, client_body)
|
|
|
|
| 73 |
# 将User-Agent改为curl/8.7.1,以模拟curl请求
|
| 74 |
client_headers["User-Agent"] = "curl/8.7.1"
|
| 75 |
|
| 76 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# api_key_id = api_key_info.get('api_key_id')
|
| 78 |
|
| 79 |
print('\n\n-----------')
|
| 80 |
print(client_headers)
|
| 81 |
print('-----------\n\n')
|
| 82 |
|
| 83 |
+
# 调用 do_proxy 并返回其结果,此处传的 headers 是不带 api_key 的数据
|
| 84 |
return await do_proxy(real_url, request.method, client_headers, client_body)
|
proxy copy 2.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import httpx
|
| 2 |
+
import asyncio
|
| 3 |
+
from starlette.responses import Response
|
| 4 |
+
|
| 5 |
+
async def do_proxy(url: str, method: str, headers: dict, content: str, max_retries: int = 3):
|
| 6 |
+
print("Proxy service started.", url)
|
| 7 |
+
client = None
|
| 8 |
+
try:
|
| 9 |
+
async with httpx.AsyncClient() as client:
|
| 10 |
+
for attempt in range(max_retries):
|
| 11 |
+
response = await client.request(
|
| 12 |
+
method=method,
|
| 13 |
+
url=url,
|
| 14 |
+
headers=headers,
|
| 15 |
+
content=content,
|
| 16 |
+
timeout=30
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# 关键:显式检查状态码
|
| 20 |
+
if response.status_code == 429:
|
| 21 |
+
retry_after = response.headers.get("Retry-After", "5") # 默认5秒
|
| 22 |
+
wait_time = int(retry_after) + attempt * 2 # 指数退避基础值
|
| 23 |
+
print(f"⚠️ 429错误!{wait_time}秒后重试 (尝试:{attempt+1})")
|
| 24 |
+
await asyncio.sleep(wait_time)
|
| 25 |
+
continue
|
| 26 |
+
|
| 27 |
+
response.raise_for_status() # 触发其他4xx/5xx异常
|
| 28 |
+
return Response(
|
| 29 |
+
content=response.content,
|
| 30 |
+
status_code=response.status_code,
|
| 31 |
+
headers=dict(response.headers)
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
except httpx.HTTPStatusError as e:
|
| 35 |
+
print(f"🚨 服务器错误 {e.response.status_code}: {e.request.url}")
|
| 36 |
+
return Response(content=str(e), status_code=e.response.status_code)
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"🔥 致命错误: {type(e).__name__}: {str(e)}")
|
| 40 |
+
return Response(content="Internal Server Error", status_code=500)
|
| 41 |
+
|
| 42 |
+
finally:
|
| 43 |
+
if client:
|
| 44 |
+
await client.aclose() # 确保连接关闭
|
proxy.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import httpx
|
| 2 |
import asyncio
|
| 3 |
from starlette.responses import Response
|
|
|
|
| 4 |
|
| 5 |
async def do_proxy(url: str, method: str, headers: dict, content: str, max_retries: int = 3):
|
| 6 |
print("Proxy service started.", url)
|
|
@@ -8,6 +9,13 @@ async def do_proxy(url: str, method: str, headers: dict, content: str, max_retri
|
|
| 8 |
try:
|
| 9 |
async with httpx.AsyncClient() as client:
|
| 10 |
for attempt in range(max_retries):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
response = await client.request(
|
| 12 |
method=method,
|
| 13 |
url=url,
|
|
|
|
| 1 |
import httpx
|
| 2 |
import asyncio
|
| 3 |
from starlette.responses import Response
|
| 4 |
+
from api_key_sb import get_api_key_info
|
| 5 |
|
| 6 |
async def do_proxy(url: str, method: str, headers: dict, content: str, max_retries: int = 3):
|
| 7 |
print("Proxy service started.", url)
|
|
|
|
| 9 |
try:
|
| 10 |
async with httpx.AsyncClient() as client:
|
| 11 |
for attempt in range(max_retries):
|
| 12 |
+
|
| 13 |
+
# 获取API密钥信息
|
| 14 |
+
api_key_info = await get_api_key_info('gemini-2.5-flash')
|
| 15 |
+
api_key = api_key_info.get('api_key')
|
| 16 |
+
api_key_show = api_key[:5]+'*****'+api_key[-5:]
|
| 17 |
+
print(f"使用API密钥:{api_key_show}")
|
| 18 |
+
headers["Authorization"] = f"Bearer {api_key}"
|
| 19 |
response = await client.request(
|
| 20 |
method=method,
|
| 21 |
url=url,
|