Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5
Webserver module to listen for webhooks and forward requests to a predefined function.
License
NotificationsYou must be signed in to change notification settings
toddrob99/Webhooks
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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
- Define a function to process requests
requestparameter will be a cherrypy request object*argsparameter will be a tuple of URL path components**kwargsparameter will be a dictionary of URL parameters- Get the body of a
POSTrequest fromrequest.body.readpassing the length ofrequest.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 jsonbody_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)
- sslModule = Select which SSL library to use (default: 'builtin')
- sslCert = Path to the certificate (SSL is disabled when empty)
- sslPrivKey = Path to the certificate's private key (SSL is disabled when empty)
- sslCertChain = Path to the full certificate chainseehttps://cherrypydocrework.readthedocs.io/deploy.html#ssl-support for more information on SSL support
- Start the Listener
- Keep your application running so the Listener can run in a separate thread
import timeimport webhook_listenerdef 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! # ... returnwebhooks = webhook_listener.Listener(handlers={"POST": process_post_request})webhooks.start()while True: print("Still alive...") time.sleep(300)About
Webserver module to listen for webhooks and forward requests to a predefined function.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.