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})
posted on 2016-03-17 17:00 北京涛子 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liujitao79/p/5288110.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值