-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb.py
118 lines (93 loc) · 3.23 KB
/
web.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Basic template for gevent+flask web server
Author: Ivan Ribeiro Rocha ([email protected])
"""
import gevent
from gevent import monkey
monkey.patch_all()
from gevent.pywsgi import WSGIServer
from flask import Flask, Response, request, abort, jsonify
from functools import wraps
import argparse, json, pymysql, redis
import logging.handlers
import requests
import requests.packages
requests.packages.urllib3.disable_warnings()
LOG_LEVEL = logging.DEBUG
LOG_FORMAT = '[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d] %(message)s'
SYSLOG = logging.handlers.SysLogHandler(address='/dev/log')
SYSLOG.setFormatter(logging.Formatter(LOG_FORMAT))
logging.basicConfig(format=LOG_FORMAT)
logging.getLogger().setLevel(LOG_LEVEL)
app = Flask(__name__)
def returns_json(f):
@wraps(f)
def decorated_function(*args, **kwargs):
r = f(*args, **kwargs)
return Response(json.dumps(r).encode('utf-8'), content_type='application/json; charset=utf-8')
return decorated_function
@app.errorhandler(404)
def page_not_found(e):
return jsonify(error=404, text=str(e)), 404
@app.errorhandler(500)
def internal_server_error(e):
return jsonify(error=500, text=str(e)), 500
@app.errorhandler(503)
def internal_server_error(e):
return jsonify(error=503, text=str(e)), 503
POOL = redis.ConnectionPool(host='localhost', port=6379,
db=0, max_connections=100)
def redis_exec():
"""
retrieve redis info array
"""
connection = redis.Redis(connection_pool=POOL)
return connection.info()
def mysql_exec():
"""
retrieve mysql hosts
"""
dbase = pymysql.connect(host='127.0.0.1', port=3306,
user='root', passwd='mysql', db='mysql')
result = dbase.cursor()
result.execute("SELECT Host FROM user WHERE User = 'root' LIMIT 1")
res = []
for host in result:
res.append(host[0])
result.close()
dbase.close()
return res
@app.route('/myroute', methods=['POST', 'GET'])
@app.route('/myroute/<name>', methods=['GET'])
@returns_json
def myroute(name=None):
gev1 = gevent.spawn(redis_exec)
gev2 = gevent.spawn(mysql_exec)
gevent.joinall([gev1, gev2])
if gev1.value == None or gev2.value == None:
abort(503)
else:
return {"redis": gev1.value["redis_version"],
"mysql":gev2.value,
"name":name}
if __name__ == "__main__":
PARSER = argparse.ArgumentParser()
PARSER.add_argument('-s', '--syslog',
dest="syslog",
action='store_true',
help="enable syslog (default: disabled)")
PARSER.add_argument('-b', '--bind',
dest="bind",
default="0.0.0.0",
help="bind address (default: 0.0.0.0)")
PARSER.add_argument('-p', '--port',
dest="port",
type=int,
default=8000,
help="listen port (default: 8000)")
ARGS = PARSER.parse_args()
if ARGS.syslog:
logging.getLogger().addHandler(SYSLOG)
logging.info('ACICTL started on %s:%d...' % (ARGS.bind, ARGS.port))
app.debug = True
WSGIServer((ARGS.bind, ARGS.port), app).serve_forever()