Created
March 2, 2021 08:48
-
-
Save defanator/9040fb958619538f8523dff506d08a3a to your computer and use it in GitHub Desktop.
fake wsgi app to act as a sentry API receiver to debug raven/sentry-sdk locally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import ujson | |
import zlib | |
import gzip | |
SVCPORT = 9999 | |
def application(environ, start_response): | |
environ_summary = "ENVIRON:\n\n" + "\n".join(["{0}: {1}".format(k, environ[k]) for k in sorted(environ.keys())]) + "\n" | |
log = environ['wsgi.errors'] | |
body = '' | |
try: | |
length = int(environ.get('CONTENT_LENGTH', '0')) | |
except ValueError: | |
length = 0 | |
if length != 0: | |
body = environ['wsgi.input'].read(length) | |
try: | |
j = gzip.decompress(body) | |
except: | |
j = body | |
pass | |
try: | |
decoded_body = ujson.decode(j) | |
except ValueError: | |
decoded_body = j | |
pass | |
response_body = b"OK\n" | |
try: | |
log.write("body was: '%s'\n" % decoded_body.decode('utf-8')) | |
except: | |
log.write("body was: '%s'\n" % ujson.dumps(decoded_body)) | |
else: | |
response_body = environ_summary.encode('utf-8') | |
status = '200 OK' | |
response_headers = [('Content-Type', 'text/plain'), | |
('Content-Length', str(len(response_body)))] | |
start_response(status, response_headers) | |
return [response_body] | |
if __name__ == '__main__': | |
try: | |
from wsgiref.simple_server import make_server | |
httpd = make_server('', SVCPORT, application) | |
print('Serving on port %d...' % SVCPORT) | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
print('Goodbye.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment