flask页面操作gpn接口
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接口的更多相关文章
- day99:MoFang:Flask-JSONRPC提供RPC接口&在APP进行窗口页面操作(窗口-帧-帧组)
目录 1.服务端基于Flask-JSONRPC提供RPC接口 1.Flask-JSONRPC简介 2.安装Flask-JSONRPC模块 3.快速实现一个测试的RPC接口 4.移动端访问测试接口 2. ...
- 父页面操作iframe子页面的安全漏洞及跨域限制问题
一.父子交互的跨域限制 同域情况下,父页面和子页面可以通过iframe.contentDocument或者parent.document来交互(彼此做DOM操作等,如父页面往子页面注入css). 跨域 ...
- 笔记-flask基础操作
笔记-flask基础操作 1. 前言 本文为flask基础学习及操作笔记,主要内容为flask基础操作及相关代码. 2. 开发环境配置 2.1. 编译环境准备 安装相关Lib ...
- 关于网站登录后的页面操作所携带的不同cookie值
对于课堂派网站,登录后的页面操作只需要携带PHPSESSID或者cookie中间那部分即可,两个都带也可,SERVERID不知道是干啥的,每次响应的都会变. 代码实现: cookie = None c ...
- JavaWeb学习记录(十二)——商城购物之数据库操作的接口定义
一.基本接口,该项目中所有接口都继承它 package blank.dao; import java.util.List; public interface BaseDao<T,PK> { ...
- 【Egret】实现web页面操作PC端本地文件操作
Egret 实现web页面操作PC端本地文件操作: http://edn.egret.com/cn/book/page/pid/181 //------------------------------ ...
- Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级
1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...
- 在子页面操作父页面元素和iframe说明
实现功能:在子页面操作父页面元素. 在实际编码的过程中,大家一定有这种需求:在父级页面有一个<iframe scrolling='auto'></iframe>内联框架,而我们 ...
- 高并发分布式系统中生成全局唯一(订单号)Id js返回上一页并刷新、返回上一页、自动刷新页面 父页面操作嵌套iframe子页面的HTML标签元素 .net判断System.Data.DataRow中是否包含某列 .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数
高并发分布式系统中生成全局唯一(订单号)Id 1.GUID数据因毫无规律可言造成索引效率低下,影响了系统的性能,那么通过组合的方式,保留GUID的10个字节,用另6个字节表示GUID生成的时间(D ...
随机推荐
- Form_Form标准控件Folder开发解析(案列)
2014-01-09 Created By BaoXinjian 1. 打开APPSTAND.fmb, 并加载程序库APPFLDR.pll. 2. 基于APPSTAND.fmb生成Folder开发所需 ...
- iis7.5中使用fastcgi方式配置php5.6.5
1.下载php-5.6.5,解压到d:/servers/php.修改extension_dir,放开用到的.dll文件:修改timezone=Asia/Shanghai; 2.如果在命令行执行php ...
- 纯HTML页面为了避免频繁前后台Ajax交互方案
需求: 看这么一个简单的界面. 它有很多下拉框,下拉框中的可选项并不是固定不变的. 由于页面是静态HTML页面,不能使用后台JSP动态生成. 之前的解决方案是,页面打开后使用多个Ajax请求,获取下拉 ...
- JAVA 99乘法表实例
实例: public class Test{ public static void main(String[] args){ for(int i=1;i<=9;i++){ for(int j=1 ...
- 【jQuery】关于选择器中的 :first 、 :first-child 、 :first-of-type
[:first] <!DOCTYPE html><html lang="zh-CN"><head> <title>test&l ...
- Android之SQLite
在模拟器运行的情况下,进入cmd运行adb shell 可进入模拟器的linux系统输入 lite3 mars_test_db 可进入sqlite模式.schema或者.sch 查看有哪些表 SQLi ...
- [Flex] ButtonBar系列——flex3 皮肤和外观设置
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...
- JAVA中集合类的使用
总的说来常用的集合类有两大类:Collection 和 Map 1) Collection接口有List和Set两大类子接口,List有ArrayList.LinkedList.Vector子类,Se ...
- http://host:8399/arcgis/rest/services/ 访问不了
一.问题: 安装完arvserver后,rest服务http://host:8399/arcgis/rest/services/访问不了 二.问题原因: 查看了一下manager日志,其中记录了几个r ...
- Delphi的Socket编程步骤(repulish)
转贴自:http://topic.csdn.net/t/20010727/16/212155.html ClientSocket 和ServerSocket几个重要的属性: 1.client和se ...