跳至主要内容

使用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 开发

当你运行 fastapi dev 时,它将以开发模式运行。

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

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

fastapi 运行

当你运行 fastapi run 时,它默认以生产模式运行。

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

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

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

许可证

本项目遵循 MIT 许可协议。

项目详情


下载文件

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

源分布

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

上传时间

构建分布

fastapi_cli_slim-0.0.5-py3-none-any.whl (9.6 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

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