English 中文(简体)
Python Falcon - Middleware
  • 时间:2024-12-22

Python Falcon - Middleware


Previous Page Next Page  

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 apppcation.

A middleware works similar to hooks. However, unpke 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.

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

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

    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.

    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.

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

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

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

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

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

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

    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.

An instance of the middleware class has to be added to the Falcon apppcation object at the time of initiapzation. 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()])

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()])
Advertisements