forked from toddrob99/Webhooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
85 lines (68 loc) · 2.12 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
"""Webhook Listener Example
Author: Todd Roberts
https://github.com/toddrob99/Webhooks
"""
import sys
import os
import logging
import logging.handlers
import time
import webhook_listener
import argparse
parser = argparse.ArgumentParser(
prog="Webhook Listener", description="Start the webhook listener."
)
parser.add_argument(
"--verbose", "-v", action="store_true", dest="verbose", help="Enable debug logging."
)
parser.add_argument(
"--port",
"-p",
type=int,
nargs=1,
dest="port",
help="Port for the web server to listen on (default: 8090).",
)
args = parser.parse_args()
port = args.port[0] if args.port and args.port[0] >= 0 else 8090
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.DEBUG if args.verbose else logging.INFO)
logger = logging.getLogger("webhooks")
console = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(
"%(asctime)s :: %(levelname)8s :: %(module)s(%(lineno)d) :: %(message)s",
datefmt="%Y-%m-%d %I:%M:%S %p",
)
console.setFormatter(formatter)
logger.addHandler(console)
logPath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "logs")
if not os.path.exists(logPath):
os.makedirs(logPath)
file = logging.handlers.TimedRotatingFileHandler(
os.path.join(logPath, "webhooks.log"), when="midnight", interval=1, backupCount=7
)
file.setFormatter(formatter)
logger.addHandler(file)
logger.debug("Logging started!")
def parse_request(request, *args, **kwargs):
logger.debug(
"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(port=port, handlers={"POST": parse_request})
webhooks.start()
while True:
logger.debug("Still alive...")
time.sleep(300)