首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏自动化、性能测试

    FastAPI(62)- FastAPI 部署在 Docker

    . ├── app │ ├── __init__.py │ └── main.py ├── Dockerfile └── requirements.txt FastAPI 应用程序 main.py 代码 from typing import Optional from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root 在这一步中使用缓存会在开发过程中一次又一次地构建镜像时节省大量时间,而不是每次都下载并安装所有依赖项 Docker 缓存 这里有一个重要的技巧 Dockerfile,首先只复制依赖项的文件,而不是 FastAPI /app /code/app 在 Dockerfile 尾部,复制 FastAPI 应用程序代码 由于这是最常更改的内容,因此将其放在最后,在此步骤之后的任何内容都将无法使用缓存 构建 Docker Image 例如使用机器学习模型),并且服务器有很多 CPU 内核但内存很少,容器最终可能会使用比可用内存更多的内存,这会大大降低性能(甚至崩溃) 官方栗子 FROM tiangolo/uvicorn-gunicorn-fastapi

    4.4K20发布于 2021-10-12
  • 来自专栏xiaosen

    FastAPI入门

    pip install fastapi[all] -i https://pypi.douban.com/simple/ FastAPI案例 创建一个main.py: from fastapi import FastAPI app = FastAPI() @app.get("/") def root(): return 'hello' @app.get("/hello/{name}") from fastapi import FastAPI app = FastAPI() fake_demo_db = [{"demo_name": "Foo"}, {"demo_name": "Bar from typing import Union from fastapi import FastAPI app = FastAPI() @app.get("/demo/{demo_id}") from fastapi import FastAPI,Query from typing import List, Union app = FastAPI() @app.get("/demo/"

    70510编辑于 2024-07-25
  • 来自专栏Python研发

    fastapi(一)

    fastapi官网文档链接 创建一个main.py文件, 我这个是添加了蓝图, 关键字: from fastapi import FastAPI from text import demo from from sql_conf import models, database models.Base.metadata.create_all(bind=database.engine) app = FastAPI -*- coding: utf-8 -*- # Eamil: 1922878025@qq.com # @Author: Wyc # @Time: 3:25 下午 import jwt from fastapi import HTTPException, Security, status from fastapi.security import HTTPAuthorizationCredentials, HTTPBasic import FastAPI, Body, status, APIRouter from pydantic import BaseModel from fastapi.responses import

    1.1K10编辑于 2022-01-05
  • 来自专栏九陌斋

    FastAPI从入门到实战(0)——初识FastAPI

    本文主要介绍一下FastAPI是什么,多数内容摘自官网:https://fastapi.tiangolo.com/zh/ FastAPI是什么 FastAPI 是一个用于构建 API 的现代、快速 Starlette 特性 FastAPI 和 Starlette 完全兼容(并基于)。所以,你有的其他的 Starlette 代码也能正常工作。FastAPI 实际上是 Starlette的一个子类。 通过 FastAPI 你可以获得所有 Starlette 的特性 ( FastAPI 就像加强版的 Starlette ): 令人惊叹的性能。 通过 FastAPI 你可以获得所有 Pydantic (FastAPI 基于 Pydantic 做了所有的数据处理): 更简单: 没有新的模式定义 micro-language 需要学习。 为什么要学FastAPI 最重要就是python香啊,用python能做很多数据处理,然后python的web框架也很多,但是像FastAPI这样灵活简洁的还是极少的。

    4.5K20编辑于 2022-12-27
  • 来自专栏从零开始学自动化测试

    FastAPI学习-31 FastAPI 如何集成 socket.io

    有个 socket.io 的fastapi-socketio官方库,该库依赖传统的 python-socketio 库 环境准备 pip install fastapi-socketio fastapi 服务端代码demo from fastapi import FastAPI from fastapi_socketio import SocketManager import uvicorn app = FastAPI() socket_manager = SocketManager(app=app, mount_location="/ws") @socket_manager.on('connect Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO) 说明fastapi-socketio

    1.7K10编辑于 2024-01-29
  • 来自专栏编码视界

    FastAPI后台开发基础(20): 设置 FastAPI 元数据

    设置描述信息from __future__ import annotationsimport uuidimport uvicornfrom fastapi import FastAPIdescription = """这是我的FastAPI描述信息 # 一级标题这是一级标题下的 **内容**.## 这是二级标题description 参数中可以使用 markdown 语法,比如设置列表:* **Create app = FastAPI( title = "FastAPI Metadata 元数据设置", description = description, summary = "总结信息" 127.0.0.1', port = 18081)设置接口文档与文档 URLfrom __future__ import annotationsimport uuidimport uvicornfrom fastapi docs", "url": "https://www.apache.org/licenses/LICENSE-2.0.html", }, },]app = FastAPI

    34443编辑于 2024-11-20
  • 来自专栏飞鸟的专栏

    FastAPI基础-异步

    异步请求处理在 FastAPI 中,我们可以使用 async def 来定义异步请求处理函数。这样,我们就可以在请求处理函数中执行异步操作,例如发送异步 HTTP 请求、读写文件等。 下面是一个简单的示例:from fastapi import FastAPIimport httpxapp = FastAPI()@app.get("/")async def index(): async 在 FastAPI 中,我们可以使用异步的方式连接和访问数据库。例如,如果我们使用 PostgreSQL 数据库,可以使用 asyncpg 库来实现异步访问。

    1.3K10编辑于 2023-05-07
  • 来自专栏自动化、性能测试

    FastAPI(46)- JSONResponse

    背景 创建 FastAPI 路径操作函数时,通常可以从中返回任何数据:字典、列表、Pydantic 模型、数据库模型等 默认情况下,FastAPI 会使用 jsonable_encoder 自动将该返回值转换为 JSON 字符串 然后,FastAPI 会将与 JSON 兼容的数据(例如 dict)放在 JSONResponse 中,然后将 JSONResponse 返回给客户端 总结:默认情况下,FastAPI import FastAPI from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse from pydantic import BaseModel app = FastAPI() class Item(BaseModel): id: str name: str 是自动帮忙做了转换的 等价写法如下 from fastapi.encoders import jsonable_encoder @app.post("/item") async def get_item

    1.6K10发布于 2021-10-09
  • 来自专栏飞鸟的专栏

    FastAPI-Pydantic

    FastAPI 内置了 Pydantic,可以方便地使用 Pydantic 操作请求和响应的数据。 下面是一个使用 FastAPI 和 Pydantic 的示例:from fastapi import FastAPIfrom pydantic import BaseModelapp = FastAPI 另外,FastAPI 还提供了一些常用的 Pydantic 扩展,包括:fastapi-utils:提供了一些常用的请求和响应处理函数。fastapi-pagination:提供了分页处理的功能。 fastapi-jwt-auth:提供了 JWT 认证的功能。fastapi-mail:提供了邮件发送的功能。fastapi-cors:提供了跨域资源共享的支持。

    73710编辑于 2023-05-07
  • 来自专栏飞鸟的专栏

    FastAPI-SQLAlchemy

    FastAPI 提供了与 SQLAlchemy 的集成,可以方便地使用 SQLAlchemy ORM 操作数据库。 下面是一个使用 FastAPI 和 SQLAlchemy 的示例:from fastapi import FastAPIfrom sqlalchemy.orm import Sessionfrom sqlalchemy /test.db"engine = create_engine(SQLALCHEMY_DATABASE_URL)Base = declarative_base()app = FastAPI()@app.on_event

    1.2K10编辑于 2023-05-07
  • 来自专栏大飞的部落阁

    FastApi请求拦截

    今天我们就用非常简单的 FastApi 请求拦截例子来深入理解请求拦截。 原始代码 from fastapi import FastAPI, Request from fastapi.responses import JSONResponse import uvicorn 需求 1 假设我们现在需要向 response 中增加一个参数来告诉客户端我们这个请求处理所花费的时间,我们可以使用 FastApi 的中间件来实现。 from fastapi import FastAPI, Request from fastapi.responses import JSONResponse import uvicorn,time from fastapi import FastAPI, Request from fastapi.responses import JSONResponse,RedirectResponse import

    3.1K10编辑于 2022-06-17
  • 来自专栏自动化、性能测试

    FastAPI(36)- FastAPI 的元数据配置和文档 URL

    ://www.cnblogs.com/poloyy/ # time: 2021/9/26 8:53 下午 # file: 31_metadata.py """ import uvicorn from fastapi import FastAPI description = """ ChimichangApp API helps you do awesome stuff. ## Items You can * be able to: * **Create users** (_not implemented_). * **Read users** (_not implemented_). """ app = FastAPI import FastAPI app = FastAPI(openapi_url="/api/v1/openapi.json") @app.get("/items/") async def read_items import FastAPI app = FastAPI(docs_url="/documentation", redoc_url="/redo") @app.get("/items/") async

    1.9K10发布于 2021-09-29
  • 来自专栏编程杂记

    FastAPI】请求体

    FastAPI 中,请求体(Request Body)是通过请求发送的数据,通常用于传递客户端提交的信息。FastAPI 使得处理请求体变得非常容易。 请求体是客户端发送给 API 的数据。 具体代码例如: from typing import Union from fastapi import FastAPI from pydantic import BaseModel class description: Union[str, None] = None price: float tax: Union[float, None] = None app = FastAPI FastAPI 会识别它们中的每一个,并从正确的位置获取数据。 from typing import Union from fastapi import FastAPI from pydantic import BaseModel class Item(BaseModel

    38810编辑于 2024-01-17
  • 来自专栏Python七号

    FastAPI 接口限流

    'DEFAULT_THROTTLE_RATES': { 'anon': '100/day', 'user': '1000/day' } } 这里分享一下 FastAPI 限流的 3 个方法: 1、slowapi[2] slowapi 是有人根据 flask-limiter 改写的,计数器默认保存在内存中,具体用法如下: from fastapi import FastAPI from slowapi.util import get_remote_address limiter = Limiter(key_func=get_remote_address) app = FastAPI [3] 需要一个 redis 来保存计数器: import aioredis import uvicorn from fastapi import Depends, FastAPI from fastapi_limiter import FastAPILimiter from fastapi_limiter.depends import RateLimiter app = FastAPI() @app.on_event

    1.5K30编辑于 2022-05-24
  • 来自专栏编程杂记

    FastAPI】查询参数

    通过同样的方式,你可以将它们的默认值设置为 None 来声明可选查询参数: 例如 q: Union[str, None] = None, 如果不设置成默认值的话就会变成必填选项 多个路径和查询参数 FastAPI

    61310编辑于 2024-01-17
  • FastAPI是什么?

    FastAPI 不仅以其高性能和易用性著称,还凭借其先进的设计理念为开发者提供了一种全新的 API 构建体验。1. 什么是 FastAPIFastAPI 的核心特性 性能卓越:FastAPI 的性能与 Node.js 和 Go 相媲美,在 Python 框架中表现出色。 快速上手 FastAPI以下是一个简单的 FastAPI 应用示例,帮助你快速了解其基本用法。 安装 FastAPI 和 Uvicorn首先,通过 pip 安装 FastAPI 和 Uvicorn(一个 ASGI 服务器):bash 代码解读复制代码pip install fastapi uvicorn FastAPI 强扩展性FastAPI 不仅易于上手,还具备强大的扩展性。

    89110编辑于 2024-08-22
  • FastAPI学习笔记

    由于其出色的性能和易用性,FastAPI在近年来逐渐受到了开发者的青睐。 (学习该框架强烈建议有python基础) 一、安装FastAPI 在安装FastAPI之前: 请先前往从 Python 官方网站 下载适合您操作系统的 Python 版本。 安装 Python。 安装 FastAPI: 使用下面的命令安装FastAPI pip install fastapi (FastAPI 依赖于 Starlette 和 Uvicorn。 编写基本应用 在 main.py 中,编写以下代码: from fastapi import FastAPI app = FastAPI() @app.get("/") async def root 五、FastAPI 框架的优劣势 优势: 高性能:FastAPI 基于 Starlette,性能接近 Node.js 和 Go,是最快的 Python Web 框架之一。

    96331编辑于 2025-02-03
  • 来自专栏编程杂记

    FastAPI】路径参数

    路径参数 from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id ): return {"item_id": item_id} 其中{item_id}就为路径参数 运行以上程序当访问 :http://127.0.0.1:8000/items/fastapi时候 将会看到如下响应: {"item_id":"fastapi"} 有类型的路径参数 可以使用标准的 Python 类型标注为函数中的路径参数声明类型。 from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id:

    31510编辑于 2024-01-17
  • 来自专栏飞鸟的专栏

    FastAPI中间件

    FastAPI中间件FastAPI还支持使用中间件来在请求和响应之间添加功能。中间件是一种功能,它可以拦截HTTP请求,并在请求被处理之前或之后执行某些操作。 创建中间件在FastAPI中,我们可以使用FastAPI.middleware()方法来创建中间件。该方法接受一个异步函数作为参数,该函数将在请求被处理之前或之后执行。 例如,以下是一个记录请求日志的中间件:from fastapi import FastAPI, Requestapp = FastAPI()@app.middleware("http")async def request参数是fastapi.Request对象,它表示收到的HTTP请求。 例如,以下是将跨域头中间件添加到应用程序的示例:from fastapi.middleware.cors import CORSMiddlewareapp = FastAPI()app.add_middleware

    1.9K20编辑于 2023-05-07
  • 来自专栏飞鸟的专栏

    FastAPI基础-协程

    FastAPI 中使用协程在 FastAPI 中,我们可以使用协程来处理请求,以及执行异步操作。 下面是一个示例:from fastapi import FastAPIimport httpxapp = FastAPI()@app.get("/")async def index(): async 另外,在 FastAPI 中,我们还可以使用 async/await 语法来执行数据库操作。例如,如果我们使用 PostgreSQL 数据库,可以使用 asyncpg 库来实现异步访问。

    87710编辑于 2023-05-07
领券