需要将FastAPI服务器的绑定地址从默认的"localhost"修改为"0.0.0.0",这样就能够在本地网络上访问FastAPI服务器。具体的代码如下:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
在启动FastAPI服务器时,将host
参数设置为"0.0.0.0"即可。然后可以在本地网络上使用IP地址和端口号访问FastAPI服务器,例如:http://192.168.1.100:8000。