使用代理视图流传输 Zope HTTP 响应
我正在使用以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为有两种方法可以做到这一点。首先,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.这实际上应该是一个评论,但我还没有声誉。
我正在尝试做和你 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.