安装
pip3 install fastapi
pip3 install uvicorn[standard] # 是一个ASGI异步服务器网关接口服务器框架
pip3 install python-multipart #处理表单参数的
# 完整安装
pip install fastapi[all]
启动程序
# 命令行启动
uvicorn main:app --reload --host 0.0.0.0 --port 8888
# 代码方式
if __name__ == '__main__':
import uvicorn
# 模块名称+app实例
uvicorn.run(app='main:app', host='0.0.0.0', port=8888, reload=True)
# 任意模块名称+app实例
import os
app_model_name = os.path.basename(__file__).replace('.py', '')
print(app_model_name)
uvicorn.run(app=f'{app_model_name}:app', host='0.0.0.0', port=8888, reload=True)
简单入门示例
from fastapi import FastAPI
app = FastAPI()
@app.get('/index')
def index():
return {"message:" "hello world"}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app='main:app', host='0.0.0.0', port=8888, reload=True)
"""命令行启动
uvicorn main:app --reload --host 0.0.0.0 --port 8888
uvicorn main:app --workers 5 --reload #指定线程数量
"""
标签:__,示例,--,FastAPI,app,uvicorn,0.0,main,安装
From: https://www.cnblogs.com/weiweivip666/p/18040971