zoukankan      html  css  js  c++  java
  • 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})
    
  • 相关阅读:
    [SAP BASIS]sap logon error "rabax during sapgui logon"
    [ORACLE]获取当前会话的跟踪文件路径
    [ORACLE] oracle Buffer Cache 之Hash Bucket与Hash Chain List(cache bufferschain)等待事件latch:cache buffers chains
    [ORACLE]数据字典 x$kvit
    [oracle]Buffer Cache 原理
    [ORACLE]Oracle数据字典
    [oracle]Oracle查询表空间的每日增长量
    [ORACLE]Oracle等待事件
    [ORACLE] oracle sql执行过程发生的等待事件
    [SAP BASIS]SAP BASIS 升级的stack文件解析
  • 原文地址:https://www.cnblogs.com/liujitao79/p/5288110.html
Copyright © 2011-2022 走看看