Python 专家级开发实战(上篇)

深入企业级开发核心技术栈,构建高性能、可维护的 Python 应用。


31. 使用 FastAPI 构建高性能 Web API

RESTful 接口开发
from fastapi import FastAPI  
from pydantic import BaseModel  

app = FastAPI()  

class Item(BaseModel):  
    name: str  
    price: float  

@app.post("/items/")  
async def create_item(item: Item):  
    return {"item_name": item.name, "total_price": item.price * 1.1}  

@app.get("/items/{item_id}")  
async def read_item(item_id: int, q: str = None):  
    return {"item_id": item_id, "q": q}  
启动服务
uvicorn main:app --reload  # 访问 http:/

你可能感兴趣的:(python一日知识,python,开发语言)