Python スクリプトの出力を、印刷された Web ページにストリーミングしようとしています。
したがって、私の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
していますが、私が得るのは0 0 1 0 1 2 0 1 2 3 0 1 2 3 4
私が本当に欲しいのは、onreadystatechangeごとに1桁を送信することである場合、毎回バッファ全体を送信しているようです。
私は何を間違っていますか?