Package: wsgi_listener
WSGI ListenerRequest/Response Inspection for WSGI Web ApplicationsWSGI 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-listenerOriginal project's Homepage:https://github.com/pklaus/wsgi-request-loggerEasily add loggers, emailers, event systems, etc, with therequest_listenersandresponse_listenerhooks.Installationtodo: Ship to pypi
Simply install this Python module viapip install wsgi-listenerUsageTo add this middleware to your WSGIapplicationwith the default response logger namedwsgilistenerin Apache format.fromwsgi_listenerimportWSGIListenerMiddlewaredefapplication(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__':fromwsgiref.simple_serverimportmake_serverhttp=make_server('',8080,loggingapp)http.serve_forever()Custom handlersThe interface for the Request listeners is:fromabcimportABC,abstractmethodclassAbstractBaseRequestListener(ABC):@abstractmethoddefhandle(self,environ:dict,request_body:bytes,**kwargs)->None:"""Defines the interface for Request listeners.Args:environ: The WSGI envion dictionaryrequest_body: The bytes content of the request, if any**kwargs: Optional hook for additional data"""and the interface for Response listeners is:fromabcimportABC,abstractmethodclassAbstractBaseResponseListener(ABC):@abstractmethoddefhandle(self,status_code:int,environ:dict,content_length:int,response_body:bytes,processing_time:float,**kwargs)->None:"""Defines the interface for Response listeners.Args:status_code: HTTP status code as integerenviron: WSGI environ dictionarycontent_length: Number of bytes returned as intresponse_body: The response content, if anyprocessing_time: The time in miliseconds to process the request**kwargs: Extensible hook"""Simply instantiate your hooks and add them during init or with theadd_listenermethods.The AuthorsThis WSGI middleware was originally developed under the namewsgilogbyL. C. Rees.
It was forked byPhilipp Klausin 2013 to build a WSGI middleware for request logging rather than exception handling and logging,
and then forked again byJonny Fullerin 2019 to add the additional layer of indirection.LicenseThis software,wsgi-listener, is published under a3-clause BSD license.Developers' ResourcesTheWSGI- Web Server Gateway Interface - is defined inPEP 333with an update for Python 3 inPEP 3333.General ReferencesPyPI'slisting of wsgi-request-loggerThis fork source code is hosted at Github (todo determine url)The original source code for this Python module ishosted on Github.A blog post with more background information and usage examples:wsgi-request-logger – Logging HTTP Requests With Any WSGI Web Application like Flask, Bottle or Django