Spaces:
Sleeping
Sleeping
update
Browse files- proxy copy.py +24 -0
- proxy.py +39 -19
proxy copy.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi.responses import Response # 导入Response
|
| 2 |
+
import httpx # 使用httpx替代requests,因为requests是同步的,而FastAPI是异步的
|
| 3 |
+
|
| 4 |
+
async def do_proxy(url:str, method:str, headers:dict, content:str):
|
| 5 |
+
print("Proxy service started.", url)
|
| 6 |
+
try:
|
| 7 |
+
# 使用httpx异步客户端发起请求
|
| 8 |
+
async with httpx.AsyncClient() as client:
|
| 9 |
+
response = await client.request(
|
| 10 |
+
method=method,
|
| 11 |
+
url=url,
|
| 12 |
+
headers=headers,
|
| 13 |
+
content=content, # httpx使用content参数传递请求体
|
| 14 |
+
timeout=30 # 超时时间,可调整
|
| 15 |
+
)
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print("Error:", e)
|
| 18 |
+
|
| 19 |
+
# 将Gemini的响应透传给客户端
|
| 20 |
+
return Response(
|
| 21 |
+
content=response.content,
|
| 22 |
+
status_code=response.status_code,
|
| 23 |
+
headers=dict(response.headers)
|
| 24 |
+
)
|
proxy.py
CHANGED
|
@@ -1,24 +1,44 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
|
|
|
| 3 |
|
| 4 |
-
async def do_proxy(url:str, method:str, headers:dict, content:str):
|
| 5 |
print("Proxy service started.", url)
|
|
|
|
| 6 |
try:
|
| 7 |
-
# 使用httpx异步客户端发起请求
|
| 8 |
async with httpx.AsyncClient() as client:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
except Exception as e:
|
| 17 |
-
print("
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
headers=dict(response.headers)
|
| 24 |
-
)
|
|
|
|
| 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() # 确保连接关闭
|
|
|
|
|
|