flask页面操作gpn接口
生活随笔
收集整理的這篇文章主要介紹了
flask页面操作gpn接口
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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"> 關(guān)閉<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當(dāng)前狀態(tài)$.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設(shè)置$('#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當(dāng)前狀態(tài)'''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():'''設(shè)置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 tokenheaders = {'username': username, 'password': password}req = urllib2.Request(url_token, headers=headers)resp = urllib2.urlopen(req).read()token = json.loads(resp)['Access-Token']# set qosdata = {'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_qosstatus = json.loads(resp)['status']message = json.loads(resp)['messsage']except urllib2.HTTPError, e:bandwidth = old_qosstatus = 'failure'message = e.codeexcept urllib2.URLError, e:bandwidth = old_qosstatus = 'failure'message = e.reason# submit datagpn = GPN()gpn.bandwidth = bandwidthgpn.updated_user = current_user.namegpn.updated_time = datetime.datetime.now()gpn.status = statusgpn.message = messagedb.session.add(gpn)db.session.commit()return json.dumps({'current_qos': bandwidth}) posted on 2016-03-17 17:00 北京濤子 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/liujitao79/p/5288110.html
總結(jié)
以上是生活随笔為你收集整理的flask页面操作gpn接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FGO中阿比盖尔怎么打
- 下一篇: Jmeter进行分布式性能测试