WSGI listener middleware for WSGI Web Applications inspired by the original wsgi-request-logger by Philipp Klaus and L. C. Rees.
Provides hooks during the request and response cycle by adding an extra level of indirection.
Instead of directly logging the response, this middleware provides an interface to easily inspect the request and response. The default behavior logs the response similarly to the original project. However, now additional listeners can be added to both the request and response cycle. The request and response body content is part of the interface.
Project Homepage: https://github.com/JonnyWaffles/wsgi-listener Original project's Homepage: https://github.com/pklaus/wsgi-request-logger
Easily add loggers, emailers, event systems, etc, with the request_listeners
and response_listener
hooks.
todo: Ship to pypi Simply install this Python module via
pip install wsgi-listener
To add this middleware to your WSGI application
with the default response logger named wsgilistener
in Apache format.
from wsgi_listener import WSGIListenerMiddleware
def application(environ, start_response):
response_body = 'The request method was %s' % environ['REQUEST_METHOD']
response_body = response_body.encode('utf-8')
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
start_response('200 OK', response_headers)
return [response_body]
loggingapp = WSGIListenerMiddleware(application)
if __name__ == '__main__':
from wsgiref.simple_server import make_server
http = make_server('', 8080, loggingapp)
http.serve_forever()
The interface for the Request listeners is:
from abc import ABC, abstractmethod
class AbstractBaseRequestListener(ABC):
@abstractmethod
def handle(self, environ: dict, request_body: bytes, **kwargs) -> None:
"""Defines the interface for Request listeners.
Args:
environ: The WSGI envion dictionary
request_body: The bytes content of the request, if any
**kwargs: Optional hook for additional data
"""
and the interface for Response listeners is:
from abc import ABC, abstractmethod
class AbstractBaseResponseListener(ABC):
@abstractmethod