Skip to main content

WSGI listener middleware for WSGI Web Applications inspired by the

Project description

WSGI Listener

Request/Response Inspection for WSGI Web Applications

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.

Installation

todo: Ship to pypi Simply install this Python module via

pip install wsgi-listener

Usage

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

Custom handlers

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
    def handle(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 integer
            environ: WSGI environ dictionary
            content_length: Number of bytes returned as int
            response_body: The response content, if any
            processing_time: The time in miliseconds to process the request
            **kwargs: Extensible hook
        """

Simply instantiate your hooks and add them during init or with the add_listener methods.

The Authors

This WSGI middleware was originally developed under the name wsgilog by L. C. Rees. It was forked by Philipp Klaus in 2013 to build a WSGI middleware for request logging rather than exception handling and logging, and then forked again by Jonny Fuller in 2019 to add the additional layer of indirection.

License

This software, wsgi-listener, is published under a 3-clause BSD license.

Developers' Resources

  • The WSGI - Web Server Gateway Interface - is defined in PEP 333 with an update for Python 3 in PEP 3333.

General References

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wsgi_listener-0.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wsgi_listener-0.1-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file wsgi_listener-0.1.tar.gz.

File metadata

  • Download URL: wsgi_listener-0.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for wsgi_listener-0.1.tar.gz
Algorithm Hash digest
SHA256 05432bf8947e2dcfccfac259bcb5d9010688c0388648a0e791c4429f4f855211
MD5 8478e68648c1d34e368e6624fe5c735e
BLAKE2b-256 9564339a2383a35aa9cf95b7e99abc804bd7c643905ae6d72c3b6373175172b3

See more details on using hashes here.

File details

Details for the file wsgi_listener-0.1-py3-none-any.whl.

File metadata

  • Download URL: wsgi_listener-0.1-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for wsgi_listener-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c531168d67176a1594987961c1f00461c39f8960704bf829b843b1aaf024dd2f
MD5 95f8f6c8433b4722cb6e877251a7010a
BLAKE2b-256 a3c77930b73acb524b14505c9d53ec88ac12d5a14e5bdf0695c550a59a95db2b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page