客至汲泉烹茶, 抚琴听者知音

【译】11个新的python web框架

原文地址

python已经成为一个构建web服务的热门语言,从粗糙(quick and dirty)的RESTful APIs到成熟的web应用,python正在为数百万计的用户服务。如果你涉及到这些这些领域,你可能已经开始使用某些最热门的web框架了—Django, Flask, Falcon, Tornado, CherryPy等等。

但是,过去几年内,一些新的项目正在涌现(many new kids on the block),这些新框架采用了一种全新的方法,重点关注API的性能和表达能力。这是11个最新的Python Web框架的清单,您可以在下一个项目中考虑这些框架。

1.Sanic

Sanic称自己为Web服务器和Web框架,代码编写起来很快。它允许使用Python 3.5中添加的async/ await语法(异步/协程),这使您的代码非阻塞(non-blocking)且快速。Sanic利用uvloopujson提高性能,但这些软件包是可选的。

安装

pip install sanic

hello world例子

from sanic import Sanic
from sanic.response import json

app = Sanic()

@app.route('/')
async def test(request):
    return json({'hello': 'world'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

Sanic的github地址

2.Starlette

Starlette是一种轻量级的ASGI框架,是构建高性能asyncio服务的理想之选,可以用作完整框架或ASGI工具包。配合一些标准库(batteries included),它支持WebSockets,GraphQL,进程内后台任务以及基于request构建的测试客户端。

安装

pip install starlette

hello world例子

from starlette.applications import Starlette
from starlette.responses import JSONResponse
import uvicorn

app = Starlette(debug=True)


@app.route('/')
async def homepage(request):
    return JSONResponse({'hello': 'world'})

if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8000)

Starlette的github地址

3.Masonite

Masonite是一个以开发人员为中心的Python Web框架,致力于实用的开发者工具标准库,它具有许多现成的功能以及完全可扩展的体系结构。它同样有一个简单而高性能的路由引擎,一个简单的迁移系统(该迁移系统消除了迁移的复杂性),以及一个称为Orator的出色Active Record风格的ORM(对象-关系映射)。

安装

pip install masonite-cli

Masonite的github地址

4.FastAPI

FastAPI是一个现代的高性能Web框架,基于标准Python类型提示,并使用Python 3.6+构建API。它建立在Starlette的基础上,并且是最快的Python框架之一。基于并且完全兼容API的开放标准— OpenAPI(以前称为Swagger)和JSON Schema

安装

pip install fastapi

hello world例子

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}

FastAPI的github地址

5.Responder

同样基于Starlette,Responder的主要理念是将Flask和Falcon的优雅之处整合到一个框架中。它具有内置的生产静态文件服务器,自动压缩响应,本地GraphQL支持以及一个使用request的内置测试客户端。

安装

pip install responder

hello world例子

import responder

api = responder.API()

@api.route("/{greeting}")
async def greet_world(req, resp, *, greeting):
    resp.text = f"{greeting}, world!"

if __name__ == '__main__':
    api.run()

Responder的github地址

6.Molten

Molten是用于Python构建HTTP API的最小,可扩展,快速且高效的框架。Molten可以根据预定义的模式自动验证请求,确保您的处理程序仅在给出有效输入后才运行。Molten还支持基于功能的中间件(middleware),依赖注入(Dependency Injection),并包括molten.contrib库,此库包含现实世界中的API通常所需的各种功能,例如配置文件,Prometheus metrics,请求ID,会话(sessions),SQLAlchemy,模板,Websockets等。

安装

pip install molten

hello world例子

from molten import App, Route

def hello(name: str) -> str:
    return f"Hello {name}!"

app = App(routes=[Route("/hello/{name}", hello)])

Molten的github地址

7.Japronto

Japronto是一个快速,可扩展的,异步Python 3.5+ HTTP工具箱,与基于uvlooppicohttpparser的流水线(pipelining)HTTP服务器集成在一起。它面向速度爱好者,喜欢管道(plumbing)的人和早期采用者。目前没有新的项目开发,但也没有被放弃。

安装

pip install japronto

hello world例子

from japronto import Application

def hello(request):
    return request.Response(text='Hello world!')

app = Application()
app.router.add_route('/', hello)
app.run(debug=True)

Japronto的github地址

8.Klein

Klein是一个微型框架,用于开发可用于生产环境的Web服务。它是“微型”的,因为它具有类似于Bottle和Flask的非常小的API。它不是“微型”的,因为它依赖于标准库之外的东西。这主要是因为它基于Werkzeug和Twisted等广泛使用且经过良好测试的组件。

安装

pip install klein

hello world例子

from klein import run, route

@route('/')
def home(request):
    return 'Hello, world!'

run("localhost", 8080)

Klein的github地址

9.Quart

Quart是Python ASGI微型web框架。它旨在提供最简单的方法来在Web环境中使用asyncio功能,尤其是在现有的Flask应用程序中。这是可能的,因为Quart API是Flask API的超集。Quart目标是成为一个完整的微型Web框架,因为它支持HTTP / 1.1,HTTP / 2和websocket。

安装

pip install quart

hello world例子

rom quart import Quart, websocket

app = Quart(__name__)

@app.route('/')
async def hello():
    return 'hello'

@app.websocket('/ws')
async def ws():
    while True:
        await websocket.send('hello')

app.run()

Quart的github地址

10.BlackSheep

BlackSheep是一个异步Web框架,用于构建基于事件的非阻塞Python Web应用程序。它受Flask和ASP.NET Core的启发。BlackSheep支持通过类型注释或约定自动绑定请求处理程序的值。它还支持依赖注入,并实现使用外部库处理身份验证和授权的策略。

安装

pip install blacksheep

hello world例子

from datetime import datetime
from blacksheep.server import Application
from blacksheep.server.responses import text

app = Application()

@app.route('/')
async def home(request):
    return text(f'Hello, World! {datetime.utcnow().isoformat()}')

BlackSheep的github地址

11.Cyclone

Cyclone是一个Web服务器框架,将Tornado API实现为Twisted协议。这个想法是将Tornado优雅而简单的API桥接到Twisted的Event-Loop,从而实现大量受支持的协议。这种结合为构建混合服务器提供了基础,该服务器能够非常有效地处理HTTP,同时还可以同时服务或使用电子邮件,ssh,sip,irc等。

安装

pip install cyclone

Cyclone的github地址

添加新评论