Skip to main content

Very basic webserver module to listen for webhooks and forward requests to predefined functions.

Project description

Webhook Listener

Very basic webserver module to listen for webhooks and forward requests to predefined functions.

Author: Todd Roberts

https://pypi.org/project/webhook_listener/

https://github.com/toddrob99/Webhooks

Install

Install from PyPI using pip

pip install webhook_listener

Use

  • Define a function to process requests
    • request parameter will be a cherrypy request object
    • *args parameter will be a tuple of URL path components
    • **kwargs parameter will be a dictionary of URL parameters
    • Get the body of a POST request from request.body.read passing the length of request.headers['Content-Length']: request.body.read(int(request.headers['Content-Length'])) if int(request.headers.get('Content-Length',0)) > 0 else ''
    • Note: The body will be a byte array, and not a string. You may need to decode it to a string. For example:
      import json
      body_raw = request.body.read(int(request.headers['Content-Length'])) if int(request.headers.get('Content-Length',0)) > 0 else '{}'
      body = json.loads(body_raw.decode('utf-8'))
      
  • Include webhook-listener in your project
  • Create an instance of the webhook_listener.Listener class
    • handlers = Dictionary of functions/callables for each supported HTTP method. (Example: {'POST':process_post_request, 'GET':process_get_request})
    • port = Port for the web server to listen on (default: 8090)
    • host = Host for the web server to listen on (default: '0.0.0.0')
    • threadPool = Number of worker threads for the web server (default: 10)
    • logScreen = Setting for cherrypy to log to screen (default: False)
    • autoReload = Setting for cherrypy to auto reload when python files are changed (default: False)
  • Start the Listener
  • Keep your application running so the Listener can run in a separate thread

Example

import time
import webhook_listener

def process_post_request(request, *args, **kwargs):
    print('Received request:\n' + 
                'Method: {}\n'.format(request.method) +
                'Headers: {}\n'.format(request.headers) + 
                'Args (url path): {}\n'.format(args) + 
                'Keyword Args (url parameters): {}\n'.format(kwargs) + 
                'Body: {}'.format(request.body.read(int(request.headers['Content-Length'])) if int(request.headers.get('Content-Length',0)) > 0 else '')
            )

    # Process the request!
    # ...

    return

webhooks = webhook_listener.Listener(handlers={'POST':process_post_request})
webhooks.start()

while True:
    print('Still alive...')
    time.sleep(300)

Project details


Download files

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

Source Distribution

Webhook_Listener-0.0.1.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

Webhook_Listener-0.0.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file Webhook_Listener-0.0.1.tar.gz.

File metadata

  • Download URL: Webhook_Listener-0.0.1.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for Webhook_Listener-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6fb89b5578b5c08bf420298fd3a536eb610006d08080b1f3d5452ec1c1640790
MD5 f3c118ac86e27fcd6003f8235aa6cc1e
BLAKE2b-256 0ab74473aea785eacd2ffd105e9fee4ef7c567a192c2f099005166866dd144d3

See more details on using hashes here.

File details

Details for the file Webhook_Listener-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: Webhook_Listener-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for Webhook_Listener-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8a4a9152a5a2ca106ee65254fda3bc8293a56fd407e07910a869d4bf5d6229c3
MD5 b3bc3b75b248a0d9679bf4f64eca2ccf
BLAKE2b-256 d8313661b23149f13743fd87cce636663c89bbf1f281d1144c4cb81223e51658

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