• 动态输出日志


    后端:

    def test(request):

    import os
    import time
    t = time.time()
    logfilename = time.strftime('%Y%m%d%H%M%S') + '.log'
    # print logfilename
    # status, out = os.system("/tmp/sss.sh + ' ' + 'test'>/tmp/%s" % logfilename)
    # out = os.system("/tmp/sss.sh 'test'>/tmp/%s" % logfilename)
    logfilepath = '/tmp/%s' % logfilename
    t = threading.Thread(target=shell_os, args=(logfilename,))
    t.setDaemon(True)
    t.start()
    f = file('/tmp/20160903083315.log', 'r+')
    # time.sleep(10)
    # out = f.readline()
    # pos = f.tell()
    # print out
    # f.close()
    # return HttpResponse(out)
    return render_to_response('myajax.html', {'title': logfilepath, 'output': 'test',})


    import os


    def shell_os(logfilename):
    out = os.system("/tmp/sss.sh 'test'>/tmp/%s" % logfilename)
    return out


    def myajax(request):
    # print request.method

    if request.method == 'POST':
    print request.method
    print request
    filepath = request.POST.get('filepath')
    print filepath
    pos = request.POST.get('pos')
    print pos

    f = file(filepath, 'r+')
    print '-------------'
    f.seek(int(pos))
    print '-------------'
    out = f.readline()
    pos = f.tell()
    f.close()
    print '==========out', out, pos
    ret = {'out': out, 'posh': pos}
    return HttpResponse(json.dumps(ret))
    else:
    return HttpResponse('GET')

    前端:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Test</title>
    <style>
    .top {
    margin: 20px;
    padding: 20px;
    border: 1px solid #ddd;
    }

    .commom {
    margin: 20px;
    padding: 20px;
    border: 1px solid #ddd;
    }

    </style>
    </head>
    <body>
    <div class="top">
    <div style="float: left">[正在执行]</div>
    <div style="float: left">{{ title }}</div>
    </div>
    <div class="commom">{{ output }}</div>

    </body>
    <script src="/statics/jquery/jquery-3.1.0.min.js" type=text/javascript></script>
    <script type=text/javascript>
    pos = 0;
    function myajax() {
    //console.log({{ title }})
    var filepath = "{{ title }}";

    //console.log(filepath);
    $.ajax({
    url: "/web/myajax/",
    type: "POST",
    data: {filepath: filepath, pos: pos},
    success: function (callback) {
    var obj = jQuery.parseJSON(callback);
    pos = obj.posh;
    var old_html = $(".commom").html();
    var new_html = old_html + obj.out + "<br>";
    if (obj.out.indexOf('FINISH') > 0) {
    clearInterval(logAjax)
    }
    if (obj.out == '') {
    console.log(obj.out);
    }
    else {
    $(".commom").html(new_html);

    }

    //console.log(obj.out);
    //console.log($(".commom").html())
    },
    error: function () {
    console.log('error')
    }

    })
    }

    var logAjax = setInterval('myajax()', 500);
    </script>
    </html>

    #用clearInterval(logAjax )来结束

  • 相关阅读:
    数据应用
    Python邮件脚本
    函数
    tab模块
    python登陆,注册小程序
    三元运算+lambda表达式
    计算机基础
    软件测试概要
    asyn_fifo
    perl 对ENV环境变量的使用
  • 原文地址:https://www.cnblogs.com/3one/p/5837600.html
Copyright © 2020-2023  润新知