开发者

Streaming Zope HTTP responses with proxy views

I am using the following PLone + urllib code to proxy responses from another server through a BrowserView

req = urllib2.Requ开发者_如何学Cest(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()

My question is how could I make this function not to block and start streaming the proxied response through Zope instantly when the first bytes arrive? When interfaces, objects or patterns are used in making streamable Zope responses?


I think there are two ways you can do this. Firstly, the Zope response itself is file-like so you can use the response's write() method to write successive chunks of data to the response as they come in. Here's an example where I use a Zope response as a file-like object for a csv.writer.

Or you can use ZPublisher's IStreamIterators and wrap the response in a ZPublisher.Iterators.filestream_iterator wrapper and return the wrapper.


This should actually be a comment, but I don't have the reputation yet. I am trying to do the same thing as you Mikko, and RESPONSE.write() does exactly that, as Ross said it would. Note however that the bytes won't actually leave the interface until there's 64K of them (or connection closes). Flushing stdout won't help so it seems that you will have to interfere further down with the socket to promptly send a few bytes right away.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜