使用代理视图流式传输Zope HTTP响应

Mik*_*maa 3 streaming zope plone http urllib

我使用以下PLone + urllib代码通过BrowserView代理来自其他服务器的响应

req = urllib2.Request(full_url)

    try:

        # Important or if the remote server is slow
        # all our web server threads get stuck here
        # But this is UGLY as Python does not provide per-thread
        # or per-socket timeouts thru urllib
        orignal_timeout = socket.getdefaulttimeout()
        try:
            socket.setdefaulttimeout(10)

            response = urllib2.urlopen(req)
        finally:
            # restore orignal timeoout
            socket.setdefaulttimeout(orignal_timeout)

        # XXX: How to stream respone through Zope
        # AFAIK - we cannot do it currently

        return response.read()
Run Code Online (Sandbox Code Playgroud)

我的问题是,当第一个字节到达时,我怎么能使这个功能不阻止并立即开始通过Zope流式传输代理响应?当接口,对象或模式用于制作流式Zope响应时?

Ros*_*son 5

我认为有两种方法可以做到这一点.首先,Zope响应本身就像文件一样,因此您可以使用响应的write()方法在响应进入时将连续的数据块写入响应. 这是一个示例,其中我使用Zope响应作为csv的类文件对象.作家.

或者您可以使用ZPublisher的IStreamIterator并将响应ZPublisher.Iterators.filestream_iterator包装在包装器中并返回包装器.