-
Notifications
You must be signed in to change notification settings - Fork 11
/
flask_demo.py
61 lines (46 loc) · 1.4 KB
/
flask_demo.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
# coding:UTF-8
"""
一个小demo(基于flask)
@author: yubang
创建于2017年4月13日
"""
from flask import Flask, request, g
from time import time
from datetime import datetime
from sdk import ILog
import traceback
app = Flask(__name__)
ilog = ILog('http://127.0.0.1:8000', 'abc', 'demoApp')
@app.route('/')
def index():
a = 5 / 0
return "welcome!"
@app.route('/debug')
def debug():
t = request.args.get('t', u"没有参数")
ilog.info(t)
return t
@app.before_request
def before_request():
g.ilog = {
"headers": dict(request.headers),
"param": dict(request.args) if request.method == 'GET' else dict(request.form),
"t": time(),
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"url": request.url,
"error": ''
}
@app.after_request
def after_request(d):
g.ilog['t'] = "%.2f" % (float(time() - g.ilog['t']))
g.ilog['response_data'] = d.data
print ilog.login_log(g.ilog['url'], g.ilog['param'], g.ilog['response_data'], g.ilog['time'], g.ilog['t'],
request_headers=g.ilog['headers'], request_method=request.method, status_code=d.status_code,
error_data=g.ilog['error'])
return d
@app.errorhandler(500)
def error_handlers(e):
g.ilog['error'] = traceback.format_exc()
return "error", 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9000, debug=False)