将控制台应用程序输出读取到网页
我正在开发一个django应用程序。我有一个控制台应用,它会显示一些文本。这段文本有点长,是基于一些处理过程生成的,这个控制台应用大约运行5到10分钟。
我想把整个内容捕捉到django网站上。我查阅了很多网络文章,但还是没找到一个靠谱的方法来实现这个目标。
如果我使用subprocess.check_output来运行这个应用,然后再显示结果,那就太晚了。
我知道我需要使用Ajax,但不太清楚该怎么做。
我尝试过把控制台应用的输出重定向到一个文本文件,然后用Ajax读取这个文件的内容,但这并没有成功。有没有更有效的方法来做到这一点呢?
我用下面的代码来读取文本。老实说,我对Ajax和JavaScript并不太了解。
<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>
请帮我解决这个问题。
谢谢。
相关问题:
1 个回答
0
我找到了解决办法。
在视图中,我创建了一个线程,然后启动了一个控制台应用程序,这个程序会运行5到10分钟。
接着,我提供了一个页面,上面有之前提到的ajax代码,这段代码会在文本文件更新时读取文件的内容。
谢谢,
Bala。