在flush()之后清除Python中的stdout

| 我正在尝试使我的Python脚本将其输出流打印到我的网页上。 因此,在我的JavaScript中,我这样做:
var xmlhttp;
var newbody = \"\";
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==3) {
      newbody = newbody + xmlhttp.responseText;
      document.getElementById(\"new\").innerHTML=newbody;
    }
}
xmlhttp.open(\"GET\",\"http://localhost/cgi-bin/temp.py\",true);
xmlhttp.send();
在我的Python脚本中,我有:
print \"Content-Type: text/plain\"
print \"\"
print \" \" * 5000   # garbage data for safari/chrome
sys.stdout.flush()

for i in range(0,5):
    time.sleep(.1)
    sys.stdout.write(\"%i \" % i)
    sys.stdout.flush()
现在我期望
0 1 2 3 4
,但是我得到is3ѭ 似乎每次都发送整个缓冲区,而我真正想要的是每次onreadystatechange发送一个数字。 我究竟做错了什么?     
已邀请:
        客户端上的
xmlhttp.responseText
始终包含整个响应,因此您不需要
newbody
,只需使用
xmlhttp.responseText
。     

要回复问题请先登录注册