相关疑难解决方法(0)

使用Twisted的twisted.web类,如何刷新传出缓冲区?

我使用Twisted创建了一个简单的http服务器,它发送Content-Type:multipart/x-mixed-replace标头.我正在使用它来测试我想要设置为接受长期流的http客户端.

出现的问题是我的客户端请求挂起,直到http.Request调用self.finish(),然后它立即接收所有多部分文档.

有没有办法手动将输出缓冲区刷新到客户端?我假设这就是为什么我没有收到单独的多部分文件.

#!/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 = "<html>foo</html>\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 = "<html>bar</html>\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 = "<html>baz</html>\n"
        self.write("Content-Length: %s\n\n" % (len(s)))
        self.write(s)

        self.writeStop()

        self.finish()

class StreamProtocol(http.HTTPChannel):
    requestFactory …
Run Code Online (Sandbox Code Playgroud)

python twisted multipart-mixed-replace

9
推荐指数
1
解决办法
5818
查看次数

标签 统计

multipart-mixed-replace ×1

python ×1

twisted ×1