Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
import httpx
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
@app.all("/{path:path}")
|
8 |
+
async def proxy(request: Request, path: str):
|
9 |
+
async with httpx.AsyncClient() as client:
|
10 |
+
# 使用相同的方法和路径参数发出请求
|
11 |
+
method = request.method
|
12 |
+
url = f"http://52.91.103.39:1234/{path}"
|
13 |
+
|
14 |
+
# 转发请求头(可选)
|
15 |
+
headers = dict(request.headers)
|
16 |
+
|
17 |
+
# 转发请求体(如果有的话)
|
18 |
+
content = await request.body()
|
19 |
+
|
20 |
+
# 执行代理请求
|
21 |
+
response = await client.request(
|
22 |
+
method=method,
|
23 |
+
url=url,
|
24 |
+
headers=headers,
|
25 |
+
content=content
|
26 |
+
)
|
27 |
+
|
28 |
+
# 返回相同的响应内容和状态码
|
29 |
+
return JSONResponse(content=response.json(), status_code=response.status_code)
|
30 |
+
|
31 |
+
# 启动FastAPI应用
|
32 |
+
# uvicorn main:app --reload
|