-
-
Save TheWaWaR/bd26ef76dabca2d410dd to your computer and use it in GitHub Desktop.
#!/usr/bin/env python | |
# coding: utf-8 | |
import os | |
import sys | |
import json | |
import uuid | |
import tempfile | |
from flask import Flask, request, Response, g | |
app = Flask(__name__) | |
app.debug = True | |
def save_request(uuid, request): | |
req_data = {} | |
req_data['uuid'] = uuid | |
req_data['endpoint'] = request.endpoint | |
req_data['method'] = request.method | |
req_data['cookies'] = request.cookies | |
req_data['data'] = request.data | |
req_data['headers'] = dict(request.headers) | |
req_data['headers'].pop('Cookie', None) | |
req_data['args'] = request.args | |
req_data['form'] = request.form | |
req_data['remote_addr'] = request.remote_addr | |
files = [] | |
for name, fs in request.files.items(): | |
dst = tempfile.NamedTemporaryFile() | |
fs.save(dst) | |
dst.flush() | |
filesize = os.stat(dst.name).st_size | |
dst.close() | |
files.append({'name': name, 'filename': fs.filename, 'filesize': filesize, | |
'mimetype': fs.mimetype, 'mimetype_params': fs.mimetype_params}) | |
req_data['files'] = files | |
return req_data | |
def save_response(uuid, resp): | |
resp_data = {} | |
resp_data['uuid'] = uuid | |
resp_data['status_code'] = resp.status_code | |
resp_data['status'] = resp.status | |
resp_data['headers'] = dict(resp.headers) | |
resp_data['data'] = resp.response | |
return resp_data | |
@app.before_request | |
def before_request(): | |
print(request.method, request.endpoint) | |
@app.after_request | |
def after_request(resp): | |
resp.headers.add('Access-Control-Allow-Origin', '*') | |
resp.headers.add('Access-Control-Allow-Headers', 'Content-Type, X-Token') | |
resp.headers.add('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE') | |
resp_data = save_response(g.uuid, resp) | |
print('Response:: ', json.dumps(resp_data, indent=4)) | |
return resp | |
@app.route('/log', methods=['GET', 'POST']) | |
def log(): | |
g.uuid = uuid.uuid1().hex | |
req_data = save_request(g.uuid, request) | |
resp = Response(json.dumps(req_data, indent=4, default=str), mimetype='application/json') | |
resp.set_cookie('cookie-name', value='cookie-value') | |
return resp | |
if __name__ == '__main__': | |
app.run('0.0.0.0', port=int(sys.argv[1]), debug=True) |
Hi I get this error
File "C:\Sites\hullo\backend\env\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functionsrule.endpoint
File "C:\Sites\hullo\backend\flask_app\routes_epic.py", line 65, in epic_sms_post
req_data = save_request(g.uuid, request)
File "C:\Sites\hullo\backend\flask_app\routes_epic.py", line 25, in save_request
for name, fs in request.files.iteritems():
AttributeError: 'ImmutableMultiDict' object has no attribute 'iteritems'
Thoughts?
found this solution:
in python3 , use dict.items() instead of dict.iteritems()
iteritems() was removed in python3, so you can't use this method anymore.
Hi I get this error
File "C:\Sites\hullo\backend\env\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functionsrule.endpoint
File "C:\Sites\hullo\backend\flask_app\routes_epic.py", line 65, in epic_sms_post
req_data = save_request(g.uuid, request)
File "C:\Sites\hullo\backend\flask_app\routes_epic.py", line 25, in save_request
for name, fs in request.files.iteritems():
AttributeError: 'ImmutableMultiDict' object has no attribute 'iteritems'Thoughts?
The script is 5 years old, flask API may have changed a lot.
found this solution:
in python3 , use dict.items() instead of dict.iteritems()iteritems() was removed in python3, so you can't use this method anymore.
Great, I'll update it.
also getting byte error. resolved by adding "default=str" to line 57 and 69
print ('Response:: ', json.dumps(resp_data, indent=4, default=str))
Response(json.dumps(req_data, indent=4, default=str), mimetype='application/json')
Very nice!! :)
Updated :)
Hi I get this error, any clues?
POST None
127.0.0.1 - - [18/Oct/2022 00:03:02] "POST /api/v0/registresdisp HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/xavier/Documents/Formacio/lamevaapi/env/lib/python3.9/site-packages/flask/ctx.py", line 49, in getattr
return self.dict[name]
KeyError: 'uuid'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/xavier/Documents/Formacio/lamevaapi/env/lib/python3.9/site-packages/flask/app.py", line 2091, in call
return self.wsgi_app(environ, start_response)
File "/Users/xavier/Documents/Formacio/lamevaapi/env/lib/python3.9/site-packages/flask/app.py", line 2076, in wsgi_app
response = self.handle_exception(e)
File "/Users/xavier/Documents/Formacio/lamevaapi/env/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/Users/xavier/Documents/Formacio/lamevaapi/env/lib/python3.9/site-packages/flask/app.py", line 1519, in full_dispatch_request
return self.finalize_request(rv)
File "/Users/xavier/Documents/Formacio/lamevaapi/env/lib/python3.9/site-packages/flask/app.py", line 1540, in finalize_request
response = self.process_response(response)
File "/Users/xavier/Documents/Formacio/lamevaapi/env/lib/python3.9/site-packages/flask/app.py", line 1885, in process_response
response = self.ensure_sync(func)(response)
File "/Users/xavier/Documents/Formacio/lamevaapi/flask_dump_request_response.py", line 60, in after_request
resp_data = save_response(g.uuid, resp)
File "/Users/xavier/Documents/Formacio/lamevaapi/env/lib/python3.9/site-packages/flask/ctx.py", line 51, in getattr
raise AttributeError(name) from None
AttributeError: uuid
Many thanks, bro :)