Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Twisted's twisted.web classes, how do I flush my outgoing buffers?
    text
    copied!<p>I've made a simple http server using Twisted, which sends the Content-Type: multipart/x-mixed-replace header. I'm using this to test an http client which I want to set up to accept a long-term stream.</p> <p>The problem that has arisen is that my client request hangs until the <em>http.Request</em> calls self.finish(), then it receives all multipart documents at once.</p> <p>Is there a way to manually flush the output buffers down to the client? I'm assuming this is why I'm not receiving the individual multipart documents.</p> <pre><code>#!/usr/bin/env python import time from twisted.web import http from twisted.internet import protocol class StreamHandler(http.Request): BOUNDARY = 'BOUNDARY' def writeBoundary(self): self.write("--%s\n" % (self.BOUNDARY)) def writeStop(self): self.write("--%s--\n" % (self.BOUNDARY)) def process(self): self.setHeader('Connection', 'Keep-Alive') self.setHeader('Content-Type', "multipart/x-mixed-replace;boundary=%s" % (self.BOUNDARY)) self.writeBoundary() self.write("Content-Type: text/html\n") s = "&lt;html&gt;foo&lt;/html&gt;\n" self.write("Content-Length: %s\n\n" % (len(s))) self.write(s) self.writeBoundary() time.sleep(2) self.write("Content-Type: text/html\n") s = "&lt;html&gt;bar&lt;/html&gt;\n" self.write("Content-Length: %s\n\n" % (len(s))) self.write(s) self.writeBoundary() time.sleep(2) self.write("Content-Type: text/html\n") s = "&lt;html&gt;baz&lt;/html&gt;\n" self.write("Content-Length: %s\n\n" % (len(s))) self.write(s) self.writeStop() self.finish() class StreamProtocol(http.HTTPChannel): requestFactory = StreamHandler class StreamFactory(http.HTTPFactory): protocol = StreamProtocol if __name__ == '__main__': from twisted.internet import reactor reactor.listenTCP(8800, StreamFactory()) reactor.run() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload