python middleware模块_Python aiohttp-route-middleware包_程序模块 - PyPI - Python中文网

本文介绍了Python的aiohttp-route-middleware库,该库提供了一个扩展的UrlDispatcherEx路由器,支持路由局部中间件且与现有路由器兼容。通过示例展示了如何使用中间件函数来处理请求,包括错误处理和多个处理程序的顺序调用。此外,还提供了安装方法和使用场景。
摘要由CSDN通过智能技术生成

aiohttp路由中间件

概述

扩展名为{a1},它提供路由本地中间件,同时保持与现有路由器兼容。

使用内置路由器,管理路由本地中间件的技术是生成嵌套的应用程序。

但是,嵌套的应用程序需要一个唯一的url前缀。因此无法实现以下目标:RequestMiddlewareHandlerGET /post/{id}authenticate, authorise(['post:read'])get_post

POST /post/{id}authenticate, authorise(['post:read:', 'post:write'])create_post

DELETE /post/{id}authenticate, authorise(['post:read:', 'post:write'])delete_post

此路由器允许由处理程序终止的中间件链。例如:post_app=web.Application(router=UrlDispatcherEx())post_app.router.add_get('/{id}',authenticate,authorise(['post:read']),get_posts)post_app.router.add_post('/{id}',authenticate,authorise(['post:read','post:write']),get_posts)post_app.router.add_delete('/{id}',authenticate,authorise(['post:read','post:write']),get_posts)app=web.Application()app.add_subapp('/post',post_app)

用法

基本

中间件函数不同于普通的请求处理程序,它被赋予下一个要调用的处理程序。

下面的示例演示如何将中间件添加到路由。fromaiohttpimportwebfromaiohttp_route_middlewareimportUrlDispatcherExapp=web.Application(router=UrlDispatcherEx())app.router.add_get('/',middleware1,middleware2,test)asyncdeftest(request):print("..entering handler")response=web.Response(text=f"extra_stuff=[{', '.join(request.extra_stuff)}]")print("..exiting handler")returnresponse@web.middlewareasyncdefmiddleware1(request,handler):print("entering middleware 1")request.extra_stuff=['foo']response=awaithandler(request)print("exiting middleware 1")returnresponse@web.middlewareasyncdefmiddleware2(request,handler):print(".entering middleware 2")request.extra_stuff.append('bar')response=awaithandler(request)print(".exiting middleware 2")returnresponseapp=web.Application(router=UrlDispatcherEx())app.router.add_get('/',middleware1,middleware2,test)web.run_app(app)

这将打印出以下内容:entering middleware 1

.entering middleware 2

..entering handler

..exiting handler

.exiting middleware 2

exiting middleware 1

中间件故障

中间件函数可以选择不调用下一个处理程序;例如,如果存在身份验证错误。fromaiohttpimportwebfromaiohttp_route_middlewareimportUrlDispatcherExasyncdeftest(request):returnweb.Response(text="Success")@web.middlewareasyncdefauthenticate(request,handler):returnweb.Response(body="unauthenticated",status=401)app=web.Application(router=UrlDispatcherEx())app.router.add_get('/',authenticate,test)web.run_app(app)

安装

您可以使用pip安装它:pip install aiohttp-route-middleware

详细信息

扩展提供了一个路由器UrlDispatcherEx,它从内置类UrlDispatcher扩展而来。该类可按以下方式使用:fromaiohttp_route_middlewareimportUrlDispatcherEx...app=web.Application(router=UrlDispatcherEx())

扩展允许指定多个处理程序。按顺序调用处理程序,直到处理程序返回非None响应,此时返回响应并停止执行。

这方面的一个例子可能是更新帖子评论的路径,顺序可能是:对用户进行身份验证。

检查用户是否有权发表评论。

去拿那根柱子。

发表评论。app.router.add_post('/comment?post_id=1234',authenticate,authorise,fetch_post,post_comment)

每个处理程序的编写方式与普通处理程序相同,因为它只接受一个请求参数。请求参数可以由每个处理程序修改或充实。

欢迎加入QQ群-->: 979659372

9ddc589a9bae9dd81334056da3504a2c.png

推荐PyPI第三方库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值