Spaces:
BG5
/
Running

BG5 commited on
Commit
4c34c0c
·
verified ·
1 Parent(s): 2e33d8b

Create http.py

Browse files
Files changed (1) hide show
  1. http.py +27 -0
http.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, Response, status
2
+ import httpx
3
+
4
+ app = FastAPI()
5
+ TARGET_BASE = "http://127.0.0.1:8001" # 替换为你的目标服务器
6
+
7
+ @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
8
+ async def proxy(request: Request, path: str):
9
+ if path == "":
10
+ # 拒绝根目录
11
+ return Response(content="Not Found", status_code=status.HTTP_404_NOT_FOUND)
12
+ url = f"{TARGET_BASE}/{path}"
13
+ headers = dict(request.headers)
14
+ body = await request.body()
15
+ async with httpx.AsyncClient(follow_redirects=True) as client:
16
+ resp = await client.request(
17
+ request.method,
18
+ url,
19
+ headers=headers,
20
+ content=body,
21
+ params=request.query_params
22
+ )
23
+ return Response(
24
+ content=resp.content,
25
+ status_code=resp.status_code,
26
+ headers=dict(resp.headers)
27
+ )