Pregunta

Estoy tratando de hacer que mi script de Python transmita su salida a mi página web a medida que se imprime.

Entonces en mi JavaScript lo hago:

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();

Y en mi guión de Python tengo:

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()

Ahora espero 0 1 2 3 4, pero lo que obtengo es 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4

Parece estar enviando todo el búfer cada vez, cuando lo que realmente quiero es que envíe un dígito por onreadyStateChange.

¿Qué estoy haciendo mal?

¿Fue útil?

Solución

xmlhttp.responseText En el lado del cliente siempre contiene la respuesta completa, por lo que no necesita newbody, Solo usa xmlhttp.responseText.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top