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

发布于 2024-11-28 13:33:16 字数 821 浏览 1 评论 0原文

我正在使用以下 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()

代理来自另一台服务器的响应我的问题是如何使此函数不阻止并在第一次时立即开始通过 Zope 传输代理响应字节到达?何时使用接口、对象或模式来生成可流式传输的 Zope 响应?

I am using the following PLone + urllib code to proxy responses from another server through a 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()

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2024-12-05 13:33:16

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

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

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.

偷得浮生 2024-12-05 13:33:16

这实际上应该是一个评论,但我还没有声誉。
我正在尝试做和你 Mikko 一样的事情,RESPONSE.write() 正是这样做的,正如 Ross 所说的那样。但请注意,在字节数达到 64K(或连接关闭)之前,这些字节实际上不会离开接口。刷新标准输出不会有帮助,因此看来您必须进一步干扰套接字才能立即发送几个字节。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文