跳转到主要内容

闪耀的ASGI库。

项目描述

starlette

✨闪耀的小型ASGI框架✨


Build Status Package version Supported Python Version


文档: https://www.starlette.io

源代码: https://github.com/encode/starlette


Starlette

Starlette是一个轻量级的ASGI框架/工具包,非常适合用Python构建异步Web服务。

它是生产就绪的,并提供以下功能

  • 轻量级、低复杂度的HTTP Web框架。
  • WebSocket支持。
  • 进程内后台任务。
  • 启动和关闭事件。
  • 基于httpx构建的测试客户端。
  • CORS、GZip、静态文件、流式响应。
  • 会话和Cookie支持。
  • 100%测试覆盖率。
  • 100%类型注解的代码库。
  • 硬依赖很少。
  • asynciotrio后端兼容。
  • 整体性能出色(与独立基准测试相比)

安装

$ pip install starlette

您还需要安装一个ASGI服务器,例如uvicorndaphnehypercorn

$ pip install uvicorn

示例

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route


async def homepage(request):
    return JSONResponse({'hello': 'world'})

routes = [
    Route("/", endpoint=homepage)
]

app = Starlette(debug=True, routes=routes)

然后使用Uvicorn运行应用程序

$ uvicorn example:app

查看更完整的示例,请参阅encode/starlette-example

依赖项

Starlette仅需要anyio,以下为可选依赖项

  • httpx - 如果您想使用TestClient,则需要此依赖项。
  • jinja2 - 如果您想使用Jinja2Templates,则需要此依赖项。
  • python-multipart - 如果您想支持表单解析,使用request.form(),则需要此依赖项。
  • itsdangerous - 如果您想支持SessionMiddleware,则需要此依赖项。
  • pyyaml - 如果您想支持SchemaGenerator,则需要此依赖项。

您可以使用pip install starlette[full]安装所有这些依赖项。

框架或工具包

Starlette旨在作为一个完整的框架或ASGI工具包使用。您可以使用其任何组件。

from starlette.responses import PlainTextResponse


async def app(scope, receive, send):
    assert scope['type'] == 'http'
    response = PlainTextResponse('Hello, world!')
    await response(scope, receive, send)

example.py中运行app应用程序

$ uvicorn example:app
INFO: Started server process [11509]
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

使用uvicorn--reload选项来启用代码更改时的自动重新加载。

模块化

Starlette所设计的模块化促进了构建可重用的组件,这些组件可以在任何ASGI框架之间共享。这应该能够建立一个共享中间件和可挂载应用程序的生态系统。

清晰的API分离还意味着可以更容易地独立理解每个组件。


Starlette是BSD许可代码。
精心设计与制作。

— ⭐️ —

项目详情


发布历史 发布通知 | RSS订阅

下载文件

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

源分发

starlette-0.39.2.tar.gz (2.6 MB 查看哈希值)

上传时间

构建分发

starlette-0.39.2-py3-none-any.whl (73.2 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

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