Spaces:
BG5
/
Running

bad-170 / http.py
BG5's picture
Create http.py
4c34c0c verified
raw
history blame
919 Bytes
from fastapi import FastAPI, Request, Response, status
import httpx
app = FastAPI()
TARGET_BASE = "http://127.0.0.1:8001" # 替换为你的目标服务器
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
async def proxy(request: Request, path: str):
if path == "":
# 拒绝根目录
return Response(content="Not Found", status_code=status.HTTP_404_NOT_FOUND)
url = f"{TARGET_BASE}/{path}"
headers = dict(request.headers)
body = await request.body()
async with httpx.AsyncClient(follow_redirects=True) as client:
resp = await client.request(
request.method,
url,
headers=headers,
content=body,
params=request.query_params
)
return Response(
content=resp.content,
status_code=resp.status_code,
headers=dict(resp.headers)
)