视图层的代码
from flask import request
from flask import jsonify
from flask import Blueprint
from flask import render_template
from monitor.logic import Logic
# 服务器监控的蓝图
monitor = Blueprint('monitor', __name__,
static_folder='static',
template_folder='templates',
url_prefix='/monitor')
@monitor.route('/')
def index():
""" 监控的首页 """
return render_template("monitor.html")
@monitor.route('/api/v1/search')
def api_v1_search():
""" 查询数据的接口 """
data = request.values.to_dict()
# 判断ip必传
if 'ip' not in data:
return jsonify({
'status': 400,
'message': 'ip必传',
'data': data
})
# 执行查询
try:
runner = Logic()
results = runner.search(data)
return jsonify({
'status': 0,
'message': 'success',
'data': results
})
except Exception as error:
return jsonify({
'status': 500,
'message': str(error),
'data': data
})
@monitor.route('/api/v1/ip/list')
def api_v1_ip_list():
""" 查询ip """
data = request.values.to_dict()
try:
lists = Logic().get_ip_list(data)
return jsonify({
'status': 0,
'message': 'success',
'data': lists
})
except Exception as error:
return jsonify({
'status': 500,
'message': str(error),
'data': data
})