【问题标题】:Reading console application output to webpage将控制台应用程序输出读取到网页
【发布时间】:2014-06-17 10:58:32
【问题描述】:

我正在开发一个 django 应用程序。我有一个显示一些文本的控制台应用程序。它基于某些进程的有点冗长的文本,这个控制台应用程序运行大约 5 到 10 分钟。

我想将整个内容捕获到 django 网站。我浏览了各种网络文章,但对如何实现这一点没有一个可靠的想法。

如果我使用 subprocess.check_output 运行该应用程序然后显示结果,那就太晚了。

我知道,我需要使用 Ajax。但不知道如何实现这一点。

我试过了,将控制台应用程序的输出重定向到一个文本文件。我尝试根据内容使用 ajax 来阅读它。它不工作。有没有一种有效的方法来做到这一点??

我使用下面的代码来读取文本。老实说,我不使用 Ajax 或 java 脚本。

<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
    /* Simple helper to add a div.
    type is the name of a CSS class (old/new/error).
    msg is the contents of the div */
    $("#messages").append(
        "<div class='msg "+ type +"'>"+ msg +"</div>"
    );
}

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: "/path/to/text/file/",

        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/

        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
        }
    });
};

$(document).ready(function(){
    waitForMsg(); /* Start the inital request */
});
</script>
<div class="msg"></div>

请帮我解决这个问题。

谢谢。

【问题讨论】:

  • this 完全符合您的描述
  • 看起来很相似。让我通过它。感谢您的回复。
  • 请注意上面写着while not process.poll(): 的行实际上应该是while process.poll() is None:,否则在某些情况下它不会终止。

标签: jquery python ajax django


【解决方案1】:

我找到了解决办法。

在视图中,我创建了一个线程并生成运行 5 到 10 分钟的控制台应用程序。 然后我使用上面的 ajax 代码提供一个页面,它将在文本文件更新时读取输出文本文件。

谢谢, 巴拉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多