質問

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

私が本当に望んでいるのは、Buffer全体を毎回送信しているようです。

私は何が間違っているのですか?

役に立ちましたか?

解決

xmlhttp.responseText クライアント側には常に応答全体が含まれているので、必要ありません newbody, 、ただ使用してください xmlhttp.responseText.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top