如何将FastAPI请求转发到另一台服务器?

2023-11-30

我有一个用于测试/开发目的的 FastAPI 应用程序。我想要的是,到达我的应用程序的任何请求都会自动发送到另一台服务器上的另一个应用程序,具有完全相同的参数和相同的端点。这是not重定向,因为我仍然希望应用程序像往常一样处理请求并返回值。我只想向不同服务器上的不同版本的应用程序发起类似的请求,而不需要等待其他服务器的答复,以便其他应用程序获取该请求,就像原始请求发送给它一样。

我怎样才能做到这一点?以下是我用于处理请求的示例代码:

@app.post("/my_endpoint/some_parameters")
def process_request(
    params: MyParamsClass,
    pwd: str = Depends(authenticate),
):
    # send the same request to http://my_other_url/my_endpoint/
    return_value = process_the_request(params)
    return return_value.as_json()

你可以使用AsyncClient()来自httpx库,如中所述这个答案, 也这个答案 and 这个答案(查看这些答案,了解有关下面演示的方法的更多详细信息)。你可以生成一个Client在 - 的里面startup事件处理程序,将其存储在app实例——如描述的here, 也here and here-并在每次需要时重复使用它。你可以明确地close the Client一旦你完成了它,使用shutdown事件处理程序.

工作示例

主服务器

当构建即将转发到其他服务器的请求时,主服务器使用request.stream()阅读请求body根据客户的要求,提供了async迭代器,这样如果客户端发送了一个带有大主体的请求(例如,客户端上传了一个大文件),主服务器就不必等待整个body在转发请求之前被接收并加载到内存中,如果您使用await request.body()相反,如果body无法装入 RAM。

可以用同样的方法添加多条路由/upload下面定义了一个,指定路径以及端点的 HTTP 方法。请注意,/upload下面的路线使用 Starlette'spath转换以捕获任意路径,如图所示here and here。如果您愿意,您还可以指定确切的路径参数,但如果路径参数太多,下面提供了更方便的方法。无论如何,将根据下面其他服务器中的端点来评估路径,您可以在其中显式指定路径参数。

from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
from starlette.background import BackgroundTask
import httpx

app = FastAPI()

@app.on_event('startup')
async def startup_event():
    client = httpx.AsyncClient(base_url='http://127.0.0.1:8001/')  # this is the other server
    app.state.client = client


@app.on_event('shutdown')
async def shutdown_event():
    client = app.state.client
    await client.aclose()


async def _reverse_proxy(request: Request):
    client = request.app.state.client
    url = httpx.URL(path=request.url.path, query=request.url.query.encode('utf-8'))
    req = client.build_request(
        request.method, url, headers=request.headers.raw, content=request.stream()
    )
    r = await client.send(req, stream=True)
    return StreamingResponse(
        r.aiter_raw(),
        status_code=r.status_code,
        headers=r.headers,
        background=BackgroundTask(r.aclose)
    )


app.add_route('/upload/{path:path}', _reverse_proxy, ['POST'])


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host='0.0.0.0', port=8000)

其他服务器

再次,为了简单起见,Request对象用于读取正文,但您可以定义UploadFile, Form和其他参数如常。在下面的示例中,服务器正在侦听端口8001.

from fastapi import FastAPI, Request

app = FastAPI()

@app.post('/upload/{p1}/{p2}')
async def upload(p1: str, p2: str, q1: str, request: Request):
    return {'p1': p1, 'p2': p2, 'q1': q1, 'body': await request.body()}
    
    
if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host='0.0.0.0', port=8001)

使用以下命令测试上面的示例httpx

import httpx

url = 'http://127.0.0.1:8000/upload/hello/world'
files = {'file': open('file.txt', 'rb')}
params = {'q1': 'This is a query param'}
r = httpx.post(url, params=params, files=files)
print(r.content)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将FastAPI请求转发到另一台服务器? 的相关文章

随机推荐