Very basic webserver module to listen for webhooks and forward requests to predefined functions.
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 from PyPI using pip
pip install webhook_listener
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 parametersPOST
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 ''
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'))
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)