FastApi的HelloWorld
from fastapi import FastAPI
# 创建Fastapi实例
app = FastAPI()
@app.get("/") # 指明调用方法与调用路径
async def HelloWorld():
# 实现内部逻辑
return {'hello': "world"}
完成上述代码后,文件保存为main.py, 在终端文件所在路径下输入
PS C:\Users\Samuel\Desktop> uvicorn demo:app --reload
?[32mINFO?[0m: Will watch for changes in these directories: ['C:\\Users\\Samuel\\Desktop']
?[32mINFO?[0m: Uvicorn running on ?[1mhttp://127.0.0.1:8000?[0m (Press CTRL+C to quit)
?[32mINFO?[0m: Started reloader process [?[36m?[1m19376?[0m] using ?[36m?[1mwatchgod?[0m
?[32mINFO?[0m: Started server process [?[36m18908?[0m]
?[32mINFO?[0m: Waiting for application startup.
?[32mINFO?[0m: Application startup complete.
Notes:
1> 此处为调试模式(--reload),当你的文件内容发生改变时,会自动加载最新文件内容
2> 默认访问的IP地址为127.0.0.1, 默认端口为 8000 ,且只可通过本机访问,可通过--host 和--port 重新设置,将host设置为0.0.0.0 则局域网内皆可访问
3> 更多参数参考uvicorn --help
此时访问 http://127.0.0.1:8000/docs 即可得到接口信息
学习笔记,内容来源于Fastapi社区, 侵权必删
https://fastapi.tiangolo.com/zh/fastapi-people/
网友评论