Python Web Day1part1Uvicorn是 Python 里的一个ASGI Web 服务器最常见的用途就是用来运行FastAPI / Starlette这类异步 Web 应用。使用 uvicorn main:app --reload 进行运行,uvicorn main:app --reload的核心好处是开发时改代码后不用手动重启服务。part 2app.get(/)#装饰器asyncdefroot():#响应结果return{message:Hello World}app是FastAPI实例 get是请求方法 ”/“请求路径part3路径参数url路径的一部分/book/{id} 只想唯一特定资源 get查询参数url之后的k1v1 对资源集合过滤排序分页等 get请求体参数http请求的消息体body 创建更新资源 postputapp.get(/user/{id})asyncdefget_user(id:int):return{id:id,username:str(id)}part4路径参数 类型注解Path名作用gt/ge lt/le/ /description描述min_length max_length长度限制路径参数出现在url路径中作为url的一部分使用Python原生注解和Path注解为路径参数添加类型注解app.get(/New/{id})asyncdefget_new(id:intPath(...,gt0,lt101,description新闻 1-100)):return{msg:f这是新闻{id}的信息}app.get(/NewD/{ca})asyncdefget_newD(ca:strPath(...,max_length10,min_length2)):return{msg:f这个类别是{ca}}Part5查询参数声明的参数不是路径参数时路径操作函数会把该参数自动解释为查询参数查询参数出现在url的之后使用Python原生注解和Query注解对查询参数进行类型注解app.get(/book/book_list)asyncdefget_bookList(ca:strQuery(...,min_length5,max_length255,description图书分类),price:intQuery(0,ge50,lt100,description价格)):return{ca:ca,price:price}