在VSCode下调试python FastAPI时,需要路由请求方面的帮助



这是我第一次尝试使用VSCode进行python开发,我遇到了不知道为什么的问题。

直接的问题是,我似乎无法让uvicorn将我的浏览器请求路由到我的fastAPI模块。以下是我的代码的相关部分:

import uvicorn
from fastapi import FastAPI, Header, HTTPException, Request, status, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
# initialize (FAST) API framework.
app = FastAPI()
# configure Cross-Origin Resource Sharing configuration
# TODO : Have to enter all the allowed origins
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)   
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)

################################
#   endpoint definitions       #
################################ 
# test endpoint
@app.get("/")
async def test():
x=0
return x

所以我在";x=0";statment并运行调试器,一切都很好,没有异常,uvicorn似乎在等待请求。

我的浏览器请求是http://127.0.0.1:8000/

我在终端窗口中看到的是:

INFO:     Started server process [22764]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:64541 - "GET /incentives HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:64544 - "GET / HTTP/1.1" 404 Not Found

我查阅了我能找到的文档,并尝试了一些排列,但没有成功。我从来没有达到突破点。

我也尝试过建立一个";launch.json";文件,但似乎什么都不做,似乎也没有启动uvicron任务或设置我的环境变量。

您有订购问题。您的函数永远不会被定义,因为uvicorn.run永远不会返回。首先定义功能。

最新更新