aiohttp的本地中间件路由。

aiohttp-route-middleware的Python项目详细描述


aiohttp路由中间件

概述

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

使用内置路由器,管理路由本地中间件的技术是生成嵌套的应用程序。 但是,嵌套的应用程序需要一个唯一的url前缀。因此无法实现以下目标:

RequestMiddlewareHandler
GET /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响应,此时返回响应并停止执行。

这方面的一个例子可能是更新帖子评论的路径,顺序可能是:

  1. 对用户进行身份验证。
  2. 检查用户是否有权发表评论。
  3. 去拿那根柱子。
  4. 发表评论。
app.router.add_post('/comment?post_id=1234',authenticate,authorise,fetch_post,post_comment)

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用split函数分割字符串,但没有得到期望的结果   未找到包含derby数据库嵌入架构的sql Java桌面应用程序错误   java elasticsearch vs solr用于定制全文搜索系统   java Android:创建没有startOffset的动画延迟?   java如何查看其他应用程序接收的数据?   java如何在Linux中使用D和classpath选项运行jar文件   java和域设计最佳实践   具有相同内存位置的java数组,将显示为输出   连接到java中的elasticsearch?   Java Playframework重定向到带有Json负载的外部url   java无法在Android平台上使用InputStream为蓝牙socket创建ObjectInputStream   使用POI将Excel日期转换为Java日期,年份未正确显示   oracle从数据库层还是Java层调用webservice?