跳转到主要内容

使用FastAPI CLI从命令行运行和管理FastAPI应用程序。 🚀

项目描述

FastAPI CLI

Test Publish Coverage Package version

源代码: https://github.com/fastapi/fastapi-cli


使用FastAPI CLI从命令行运行和管理FastAPI应用程序。 🚀

描述

FastAPI CLI 是一个命令行程序 fastapi,您可以使用它来提供您的FastAPI应用程序,管理您的FastAPI项目,等等。

当您安装FastAPI(例如,使用 pip install "fastapi[standard]")时,它包括一个名为 fastapi-cli 的软件包,该软件包在终端中提供 fastapi 命令。

要开发您的FastAPI应用程序,您可以使用 fastapi dev 命令

$ fastapi dev main.py
INFO     Using path main.py
INFO     Resolved absolute path /home/user/code/awesomeapp/main.py
INFO     Searching for package file structure from directories with __init__.py files
INFO     Importing from /home/user/code/awesomeapp

 ╭─ Python module file ─╮
 │                      │
 │  🐍 main.py          │
 │                      │
 ╰──────────────────────╯

INFO     Importing module main
INFO     Found importable FastAPI app

 ╭─ Importable FastAPI app ─╮
 │                          │
 │  from main import app    │
 │                          │
 ╰──────────────────────────╯

INFO     Using import string main:app

 ╭────────── FastAPI CLI - Development mode ───────────╮
 │                                                     │
 │  Serving at: http://127.0.0.1:8000                  │
 │                                                     │
 │  API docs: http://127.0.0.1:8000/docs               │
 │                                                     │
 │  Running in development mode, for production use:   │
 │                                                     │
 │  fastapi run                                        │
 │                                                     │
 ╰─────────────────────────────────────────────────────╯

INFO:     Will watch for changes in these directories: ['/home/user/code/awesomeapp']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [56345] using WatchFiles
INFO:     Started server process [56352]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

这个名为 fastapi 的命令行程序就是 FastAPI CLI

FastAPI CLI获取您的Python程序路径,并自动检测包含FastAPI的变量(通常命名为 app)以及如何导入它,然后提供服务。

对于生产,您将使用 fastapi run。 🚀

内部,FastAPI CLI 使用 Uvicorn,这是一个高性能、生产就绪的ASGI服务器。 😎

fastapi dev

当您运行 fastapi dev 时,它将在开发模式下运行。

默认情况下,它会启用 自动重载,因此当您修改代码时,它会自动重新加载服务器。这会消耗更多资源,并且可能比没有它时更不稳定,您应仅将其用于开发。

默认情况下,它将监听 IP 地址 127.0.0.1,这是您的机器与其自身通信的 IP 地址(localhost)。

fastapi run

当您运行 fastapi run 时,它将默认在生产模式下运行。

默认情况下,它将禁用 自动重载

它将监听 IP 地址 0.0.0.0,这意味着所有可用的 IP 地址,这样它就会对任何可以与该机器通信的人公开可访问。这就是您通常在生产中运行它的方式,例如,在容器中。

在大多数情况下,您会(并且应该)有一个“终止代理”在顶部处理您的 HTTPS,这取决于您如何部署应用程序,您的提供商可能已经为您做了这件事,或者您可能需要自己设置它。您可以在 FastAPI 部署文档 中了解更多信息。

许可证

本项目遵循 MIT 许可协议。

项目详情


下载文件

下载适合您平台的文件。如果您不确定该选择哪个,请了解更多关于 安装包 的信息。

源分布

fastapi_cli-0.0.5.tar.gz (15.6 kB 查看哈希值)

上传时间

构建分布

fastapi_cli-0.0.5-py3-none-any.whl (9.5 kB 查看哈希值)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面