Python Falcon 简明教程

Python Falcon - Middleware

"middleware" 是一个函数,用它来处理每个请求(在由任何特定响应程序处理之前),以及在返回每个响应之前。此函数会处理发送到应用程序的每个请求。

A "middleware" is a function that is processed with every request (before being processed by any specific responder) as well as with every response before returning it. This function takes each request that comes to your application.

中间件的作用类似于 hook。但是,与 hook 不同,中间件方法全局应用于整个应用程序。它可以通过运行其中定义的代码对请求执行某些流程,然后将请求传递给相应的操作函数进行处理。它还可以在返回生成的响应之前对其进行处理。

A middleware works similar to hooks. However, unlike hooks, middleware methods apply globally to the entire App. It may perform some process with the request by running a code defined in it, and then passes the request to be processed the corresponding operation function. It can also process the response generated by the operation function before returning it.

中间件是一个类,它实现一个或多个以下事件处理程序方法。对于 WSGI 应用程序,方法为:

A middleware is a class that implements one or more of the following even handler methods. For a WSGI app, the methods are −

  1. process_request (self, req, resp) − This method processes the request before routing it.

  2. process_resource (self, req, resp, resource, params) − processes the request after routing. A dict object representing any additional params derived from the route’s URI template fields may be passed.

  3. process_response (self, req, resp, resource, req_succeeded) − This method is for post-processing of the response (after routing). The req_succeeded parameter is True if no exceptions were raised otherwise False.

对于 ASGI 应用程序,除了上述方法外,中间件类还可以定义更多的方法。

In case of the ASGI app, in addition to the above methods, the middleware class may define some more methods.

为了考虑 ASGI 规范的可选部分寿命事件,可以包括启动和关闭事件处理程序。

To account for lifespan events, an optional part of ASGI specification, the startup and shutdown event handlers may be included.

  1. process_startup (self, scope, event) − This method processes the ASGI lifespan startup event. It is invoked when the server is ready to start up and receive connections, but before it has started to do so.

  2. process_shutdown(self, scope, event) − This method processes the ASGI lifespan shutdown event. It is invoked when the server has stopped accepting connections and closed all active connections.

由于 ASGI 应用程序还响应 Websocket 协议下的请求,因此中间件可以定义以下协程方法:

Since the ASGI application also responds to the requests under Websocket protocol, the middleware may define following coroutine methods −

  1. process_request_ws (self, req, ws) − This method processes a WebSocket handshake request before routing it.

  2. process_resource_ws (self, req, ws, resource, params) − This method processes a WebSocket handshake request after routing. A dict object derived from the route’s URI template fields may be passed to the resource’s responder.

中间件类的实例必须在初始化时添加到 Falcon 应用程序对象中。对于 WSGI Falcon 应用程序:

An instance of the middleware class has to be added to the Falcon application object at the time of initialization. For a WSGI Falcon app −

class MyMiddleware:
   def process_request(self, req, resp):
      pass
   def process_resource(self, req, resp, resource, params):
      pass
   def process_response(self, req, resp, resource, req_succeeded):
      pass
from falcon import App
app=App(middleware=[MyMiddleware()])

对于 ASGI 应用程序:

For the ASGI app −

class MyMiddleware:
   async def process_startup(self, scope, event):
      pass
   async def process_shutdown(self, scope, event):
      pass
   async def process_request(self, req, resp):
      pass
   async def process_resource(self, req, resp, resource, params):
      pass
   async def process_response(self, req, resp, resource, req_succeeded):
      pass
   async def process_request_ws(self, req, ws):
      pass
   async def process_resource_ws(self, req, ws, resource, params):
      pass
from falcon.asgi import App
app=App(middleware=[MyMiddleware()])