· 9 years ago · Jan 25, 2017, 06:46 AM
1import uuid
2import time
3from flask import Flask, request
4import sqlite3
5import json
6app = Flask(__name__)
7
8
9def initDb():
10 db = sqlite3.connect('log.sqlite')
11 cur = db.cursor()
12 cur.execute('''
13 CREATE TABLE IF NOT EXISTS items (
14 uuid VARCHAR(80) PRIMARY KEY,
15 timestamp INTEGER NOT NULL,
16 method VARCHAR(20) NOT NULL,
17 get TEXT,
18 post TEXT,
19 headers TEXT
20 )
21 ''')
22 return db
23initDb()
24
25@app.route('/', defaults={'path': '/'}, methods=['POST','GET'])
26@app.route('/<path:path>', methods=['POST','GET'])
27def log_me(path):
28 db = sqlite3.connect('log.sqlite')
29 an_id = uuid.uuid4().hex
30 a_time = int(time.time())
31 if request.method == 'POST':
32 postdata = json.dumps(str(request.data), indent=4)
33 else:
34 postdata = None
35 headers = json.dumps(dict(request.headers), indent=4)
36
37 cur = db.cursor()
38 cur.execute('''
39 INSERT INTO items(uuid,timestamp,method,get,post,headers) VALUES (?,?,?,?,?,?)''',
40 [an_id, a_time, request.method, path, postdata, headers])
41 db.commit()
42 return 'OK'
43
44app.secretkey = 'cfcc10d2-6ed1-4954-a2a0-4bbbc37af89e'
45app.debug = False
46
47if __name__ == '__main__':
48 app.run()