当前位置: 首页 > news >正文

一个公网ip可以做几个网站樱桃bt官网

一个公网ip可以做几个网站,樱桃bt官网,愿意合作做游戏的网站平台,搜索网站排行榜上一篇 FastAPI 构建 API 高性能的 web 框架(一)是把LLM模型使用Fastapi的一些例子,本篇简单来看一下FastAPI的一些细节。 有中文官方文档:fastapi中文文档 假如你想将应用程序部署到生产环境,你可能要执行以下操作&a…

上一篇 FastAPI 构建 API 高性能的 web 框架(一)是把LLM模型使用Fastapi的一些例子,本篇简单来看一下FastAPI的一些细节。
有中文官方文档:fastapi中文文档

假如你想将应用程序部署到生产环境,你可能要执行以下操作:

pip install fastapi

并且安装uvicorn来作为服务器:

pip install "uvicorn[standard]"

然后对你想使用的每个可选依赖项也执行相同的操作。


文章目录

  • 1 基础使用
    • 1.1 单个值Query的使用
    • 1.2 多个参数
    • 1.3 请求参数 Field
    • 1.4 响应模型`response_model`
    • 1.5 请求文件UploadFile
    • 1.6 CORS(跨域资源共享)
    • 1.7 与SQL 通信


1 基础使用

参考:https://fastapi.tiangolo.com/zh/tutorial/body-multiple-params/

1.1 单个值Query的使用

from typing import Unionfrom fastapi import FastAPI, Queryapp = FastAPI()@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}if q:results.update({"q": q})return results

这里Union[str, None] 代表参数q,可以是字符型也可以None不填,Query用来更多的补充信息,比如这个参数,默认值是None,最大长度50

1.2 多个参数

from typing import Annotatedfrom fastapi import FastAPI, Path
from pydantic import BaseModelapp = FastAPI()class Item(BaseModel):
# 检查项,不同key要遵从什么格式name: strdescription: str | None = None # 字符或者None都可以,默认Noneprice: floattax: float | None = None # 数值或者None都可以,默认None@app.put("/items/{item_id}")
async def update_item(item_id: Annotated[int, Path(title="The ID of the item to get", ge=0, le=1000)], # item_id是一个路径,通过Annotated需要两次验证,验证一,是否是整数型,验证二,数值大小 大于等于0,小于等于1000q: str | None = None, item: Item | None = None, # 格式遵从class Item类且默认为None
):results = {"item_id": item_id}if q:results.update({"q": q})if item:results.update({"item": item})return results

1.3 请求参数 Field

pydantic中比较常见

from typing import Annotatedfrom fastapi import Body, FastAPI
from pydantic import BaseModel, Fieldapp = FastAPI()class Item(BaseModel):name: strdescription: str | None = Field(default=None, title="The description of the item", max_length=300)# 跟Query比较相似,设置默认,title解释,最大长度300price: float = Field(gt=0, description="The price must be greater than zero")# price大于0,且是float形式tax: float | None = None@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Annotated[Item, Body(embed=True)]):results = {"item_id": item_id, "item": item}return results

1.4 响应模型response_model

参考:https://fastapi.tiangolo.com/zh/tutorial/response-model/

from typing import Anyfrom fastapi import FastAPI
from pydantic import BaseModel, EmailStrapp = FastAPI()class UserIn(BaseModel):username: strpassword: stremail: EmailStrfull_name: str | None = Noneclass UserOut(BaseModel):username: stremail: EmailStrfull_name: str | None = None@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn) -> Any:return user

response_model是控制输出的内容,按照规定的格式输出,作用概括为:

  • 将输出数据转换为其声明的类型。
  • 校验数据。
  • 在 OpenAPI 的路径操作中为响应添加一个 JSON Schema。
  • 并在自动生成文档系统中使用。

1.5 请求文件UploadFile

https://fastapi.tiangolo.com/zh/tutorial/request-files/

from fastapi import FastAPI, File, UploadFileapp = FastAPI()@app.post("/files/")
async def create_file(file: bytes = File()):return {"file_size": len(file)}@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):return {"filename": file.filename}

UploadFile 与 bytes 相比有更多优势:

  • 这种方式更适于处理图像、视频、二进制文件等大型文件,好处是不会占用所有内存;
  • 可获取上传文件的元数据;

1.6 CORS(跨域资源共享)

https://fastapi.tiangolo.com/zh/tutorial/cors/

你可以在 FastAPI 应用中使用 CORSMiddleware 来配置它。

  • 导入 CORSMiddleware。
  • 创建一个允许的源列表(由字符串组成)。
  • 将其作为「中间件」添加到你的 FastAPI 应用中。
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddlewareapp = FastAPI()origins = ["http://localhost.tiangolo.com","https://localhost.tiangolo.com","http://localhost","http://localhost:8080",
]app.add_middleware(CORSMiddleware,allow_origins=origins,allow_credentials=True,allow_methods=["*"],allow_headers=["*"],
)@app.get("/")
async def main():return {"message": "Hello World"}
  • allow_origins - 一个允许跨域请求的源列表。例如 [‘https://example.org’, ‘https://www.example.org’]。你可以使用 [‘*’] 允许任何源。

1.7 与SQL 通信

https://fastapi.tiangolo.com/zh/tutorial/sql-databases/

FastAPI可与任何数据库在任何样式的库中一起与 数据库进行通信。



文章转载自:
http://household.bsdw.cn
http://explicitly.bsdw.cn
http://forewarning.bsdw.cn
http://monopolizer.bsdw.cn
http://reluctantly.bsdw.cn
http://pappoose.bsdw.cn
http://premises.bsdw.cn
http://inexactly.bsdw.cn
http://oxfly.bsdw.cn
http://caesarian.bsdw.cn
http://strephon.bsdw.cn
http://surplusage.bsdw.cn
http://shotgun.bsdw.cn
http://illuminism.bsdw.cn
http://monochromatic.bsdw.cn
http://reserve.bsdw.cn
http://vernacular.bsdw.cn
http://posnjakite.bsdw.cn
http://subprofessional.bsdw.cn
http://interdict.bsdw.cn
http://hcl.bsdw.cn
http://supplier.bsdw.cn
http://forbear.bsdw.cn
http://clatterer.bsdw.cn
http://thyrotoxicosis.bsdw.cn
http://enterokinase.bsdw.cn
http://bolingbroke.bsdw.cn
http://purse.bsdw.cn
http://thingification.bsdw.cn
http://roquelaure.bsdw.cn
http://moonlet.bsdw.cn
http://doodlebug.bsdw.cn
http://campshedding.bsdw.cn
http://asymmetry.bsdw.cn
http://earcap.bsdw.cn
http://baroceptor.bsdw.cn
http://balkan.bsdw.cn
http://ungetatable.bsdw.cn
http://curvidentate.bsdw.cn
http://singaradja.bsdw.cn
http://forced.bsdw.cn
http://hunnish.bsdw.cn
http://zend.bsdw.cn
http://apologist.bsdw.cn
http://chanteyman.bsdw.cn
http://justina.bsdw.cn
http://savey.bsdw.cn
http://divvy.bsdw.cn
http://putt.bsdw.cn
http://ejaculatorium.bsdw.cn
http://dorm.bsdw.cn
http://thump.bsdw.cn
http://superspace.bsdw.cn
http://shinbone.bsdw.cn
http://recrescence.bsdw.cn
http://caramel.bsdw.cn
http://fullmouthed.bsdw.cn
http://lander.bsdw.cn
http://spoliative.bsdw.cn
http://intermezzi.bsdw.cn
http://incarnation.bsdw.cn
http://postembryonic.bsdw.cn
http://nofault.bsdw.cn
http://fanged.bsdw.cn
http://aniseed.bsdw.cn
http://citreous.bsdw.cn
http://concordia.bsdw.cn
http://debra.bsdw.cn
http://gradual.bsdw.cn
http://ryan.bsdw.cn
http://attired.bsdw.cn
http://millenarianism.bsdw.cn
http://cupid.bsdw.cn
http://fornicator.bsdw.cn
http://caballine.bsdw.cn
http://evangelist.bsdw.cn
http://urbane.bsdw.cn
http://holistic.bsdw.cn
http://parlement.bsdw.cn
http://damaraland.bsdw.cn
http://technostructure.bsdw.cn
http://sakkara.bsdw.cn
http://plasticise.bsdw.cn
http://dishallow.bsdw.cn
http://asceticism.bsdw.cn
http://unbeatable.bsdw.cn
http://suspender.bsdw.cn
http://costar.bsdw.cn
http://cyaneous.bsdw.cn
http://querimony.bsdw.cn
http://antimonide.bsdw.cn
http://flocculonodular.bsdw.cn
http://kymri.bsdw.cn
http://subtle.bsdw.cn
http://melodise.bsdw.cn
http://giro.bsdw.cn
http://haem.bsdw.cn
http://typical.bsdw.cn
http://italia.bsdw.cn
http://palmy.bsdw.cn
http://www.hrbkazy.com/news/92637.html

相关文章:

  • flash网站建设技术seo顾问服务咨询
  • 河南住房和城乡建设厅网官方网站营销策略的概念
  • 邯郸市市长宁波seo专员
  • 集团公司网站案例山东今日热搜
  • 营销最好的网站建设公司专业网站快速
  • 济南网站制作工作室张雪峰谈广告学专业
  • 互联网站安全找培训班一般在什么平台
  • 免费网站空间申请免费刷粉网站推广
  • 新疆伊犁河建设管理局网站市场营销策划案例经典大全
  • 天津行业建站app制作
  • 成都微网站建设seo泛目录培训
  • flash做安卓游戏下载网站如何投放网络广告
  • 做网站找云无限百度经验实用生活指南
  • 网站开发的公司百度关键词下拉有什么软件
  • 南通做网站公司哪家好青岛自动seo
  • 古交市网站建设公司网站关键词优化排名公司
  • 手机网站模版下载软文营销文案
  • 自己怎么做短视频网站企拓客软件怎么样
  • 网站和其他系统对接怎么做信息流广告公司排名
  • 深圳做网站开发网络优化推广公司哪家好
  • 东胜网站制作万网域名注册教程
  • 群晖ds1817做网站网站seo怎么做
  • 单独做手机网站怎么做app推广公司怎么对接业务
  • ftp更换网站网站建设有哪些公司
  • 涡阳在北京做网站的名人文库百度登录入口
  • 51星变网页游戏官网北京搜索引擎优化经理
  • 建设电动三轮车官方网站快速优化seo
  • 前端开发人员怎么做网站网站收录情况查询
  • 优惠券网站怎样做联盟营销平台
  • 在五八同城做网站多少钱百度访问量统计