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})

flask页面操作gpn接口的更多相关文章

  1. day99:MoFang:Flask-JSONRPC提供RPC接口&在APP进行窗口页面操作(窗口-帧-帧组)

    目录 1.服务端基于Flask-JSONRPC提供RPC接口 1.Flask-JSONRPC简介 2.安装Flask-JSONRPC模块 3.快速实现一个测试的RPC接口 4.移动端访问测试接口 2. ...

  2. 父页面操作iframe子页面的安全漏洞及跨域限制问题

    一.父子交互的跨域限制 同域情况下,父页面和子页面可以通过iframe.contentDocument或者parent.document来交互(彼此做DOM操作等,如父页面往子页面注入css). 跨域 ...

  3. 笔记-flask基础操作

    笔记-flask基础操作 1.      前言 本文为flask基础学习及操作笔记,主要内容为flask基础操作及相关代码. 2.      开发环境配置 2.1.    编译环境准备 安装相关Lib ...

  4. 关于网站登录后的页面操作所携带的不同cookie值

    对于课堂派网站,登录后的页面操作只需要携带PHPSESSID或者cookie中间那部分即可,两个都带也可,SERVERID不知道是干啥的,每次响应的都会变. 代码实现: cookie = None c ...

  5. JavaWeb学习记录(十二)——商城购物之数据库操作的接口定义

    一.基本接口,该项目中所有接口都继承它 package blank.dao; import java.util.List; public interface BaseDao<T,PK> { ...

  6. 【Egret】实现web页面操作PC端本地文件操作

    Egret 实现web页面操作PC端本地文件操作: http://edn.egret.com/cn/book/page/pid/181 //------------------------------ ...

  7. Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级

    1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...

  8. 在子页面操作父页面元素和iframe说明

    实现功能:在子页面操作父页面元素. 在实际编码的过程中,大家一定有这种需求:在父级页面有一个<iframe scrolling='auto'></iframe>内联框架,而我们 ...

  9. 高并发分布式系统中生成全局唯一(订单号)Id js返回上一页并刷新、返回上一页、自动刷新页面 父页面操作嵌套iframe子页面的HTML标签元素 .net判断System.Data.DataRow中是否包含某列 .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    高并发分布式系统中生成全局唯一(订单号)Id   1.GUID数据因毫无规律可言造成索引效率低下,影响了系统的性能,那么通过组合的方式,保留GUID的10个字节,用另6个字节表示GUID生成的时间(D ...

随机推荐

  1. C#学习笔记五: C#3.0Lambda表达式及Linq解析

    最早使用到Lambda表达式是因为一个需求:如果一个数组是:int[] s = new int[]{1,3,5,9,14,16,22};例如只想要这个数组中小于15的元素然后重新组装成一个数组或者直接 ...

  2. solr环境搭建

    介绍摘自百度百科:Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过 ...

  3. HTML document对象(2)

    五.相关元素操作: var a = document.getElementById("id");找到a: var b = a.nextSibling,找a的下一个同辈元素,注意包含 ...

  4. Existence and nonexistence results for anisotropic quasilinear elliptic equations

    Fragalà, Ilaria; Gazzola, Filippo; Kawohl, Bernd. Existence and nonexistence results for anisotropic ...

  5. Spring中IoC的入门实例

    Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...

  6. Python标准库12 数学与随机数 (math包,random包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经在Python运算中看到Python最基本的数学运算功能.此外,math包 ...

  7. SQL Server 2005中的分区表(三):将普通表转换成分区表(转)

    在设计数据库时,经常没有考虑到表分区的问题,往往在数据表承重的负担越来越重时,才会考虑到分区方式,这时,就涉及到如何将普通表转换成分区表的问题了. 那么,如何将一个普通表转换成一个分区表 呢?说到底, ...

  8. esriSRGeoCS3Type Constants

    ArcGIS Developer Help  (Geometry)     esriSRGeoCS3Type Constants More available geographic coordinat ...

  9. 业务中Spring使用

    不管是MVC框架还是DAO框架,在业务场景中能够通用的个人觉得AOP是一个重点,看是不是可以合理使用,其他的框架都是基础框架 ================================== 第一 ...

  10. Oracle 10g RAC中的DRM问题及关闭

    在RAC环境中,Oracle使用GRD(Global Resource Service)来记录各个RAC节点的资源信息,具体通过GCS(Global Cache Service)和GES(Global ...