https://wizardforcel.gitbooks.io/flask-extension-docs/content
http://cabeza.cn/blog/2016/02/28/datatable-learning-note-1/
页面
......
<table class="pure-table">
<tr>
<td><i class="fa fa-cog fa-lg"></i> 切换</td>
<td>
<input name="bandwidth" type="radio" id="radio_5" value="5"> 关闭
<input name="bandwidth" type="radio" id="radio_100" value="100"> 开启
</td>
<td>
<button id="submit" class="pure-button button-small pure-button-primary" disabled="disabled">提交</button>
</td>
</tr>
</table>
......
$(function() {
var old_qos;
// gpn当前状态
$.ajax({
type: 'GET',
url: $SCRIPT_ROOT + "{{ url_for('get_gpn_bandwidth') }}",
dataType: 'json',
contentType: 'application/json;charset=utf-8',
success: function(data){
old_qos = data.qos
$('#radio_' + old_qos).attr('checked', 'checked');
},
});
// 提交按钮激活
$('input[name=bandwidth]').change(function(){
if ( $('input[name=bandwidth]:checked').val() == old_qos ) {
$('#submit').attr('disabled', 'disabled');
} else {
$('#submit').removeAttr('disabled');
}
});
// 提交gpn设置
$('#submit').click(function(){
var new_qos = $('input[name=bandwidth]:checked').val();
data = {'old_qos': old_qos, 'new_qos': new_qos};
$.ajax({
type: 'POST',
url: $SCRIPT_ROOT + "{{ url_for('set_gpn_bandwidth') }}",
dataType: 'json',
contentType: 'application/json;charset=utf-8',
data: JSON.stringify(data),
success: function(data){
console.log(data);
window.location.href = "{{ url_for('gpn') }}";
}
});
})
});
后台
@app.route('/gpn/', methods=['GET'])
@login_required
def gpn():
gpns = GPN.query.order_by(GPN.id.desc()).limit(20)
return render_template('gpn.html', gpns=gpns)
@app.route('/json/gpn/bandwidth', methods=['GET'])
@login_required
def get_gpn_bandwidth():
'''
获取gpn当前状态
'''
gpn = GPN.query.order_by(GPN.id.desc()).first()
return json.dumps({'qos': gpn.bandwidth})
@app.route('/json/gpn/bandwidth', methods=['POST'])
@login_required
def set_gpn_bandwidth():
'''
设置gpn带宽
'''
old_qos, new_qos = int(request.json['old_qos']), int(request.json['new_qos'])
url_token = 'http://xxx.com/get_token/'
url_gpn = 'http://xxx.com/gpn/update/'
username = 'xxxx'
password = 'xxxx'
# get token
headers = {'username': username, 'password': password}
req = urllib2.Request(url_token, headers=headers)
resp = urllib2.urlopen(req).read()
token = json.loads(resp)['Access-Token']
# set qos
data = {'qos': new_qos, 'area_id': 'cn'}
headers = {'token': token, 'Content-Type': 'application/json'}
req = urllib2.Request(url_gpn, headers=headers, data=json.dumps(data))
try:
resp = urllib2.urlopen(req).read()
bandwidth = new_qos
status = json.loads(resp)['status']
message = json.loads(resp)['messsage']
except urllib2.HTTPError, e:
bandwidth = old_qos
status = 'failure'
message = e.code
except urllib2.URLError, e:
bandwidth = old_qos
status = 'failure'
message = e.reason
# submit data
gpn = GPN()
gpn.bandwidth = bandwidth
gpn.updated_user = current_user.name
gpn.updated_time = datetime.datetime.now()
gpn.status = status
gpn.message = message
db.session.add(gpn)
db.session.commit()
return json.dumps({'current_qos': bandwidth})
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
2015-03-17 Centos 6/RHEL disable the IPv6 module.
2015-03-17 centreon公司推出的check plugin pack