Spaces:
Running
Running
update
Browse files
proxy.py
CHANGED
|
@@ -3,19 +3,22 @@ import httpx # 使用httpx替代requests,因为requests是同步的,而FastA
|
|
| 3 |
|
| 4 |
async def do_proxy(url:str, method:str, headers:dict, content:str):
|
| 5 |
print("Proxy service started.", url)
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
# 将Gemini的响应透传给客户端
|
| 17 |
return Response(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
headers=dict(response.headers)
|
| 21 |
)
|
|
|
|
| 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 |
)
|