python – 使用Twisted的twisted.web类,如何刷新传出的缓冲区?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 使用Twisted的twisted.web类,如何刷新传出的缓冲区?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3098字,纯文字阅读大概需要5分钟。
内容图文
![python – 使用Twisted的twisted.web类,如何刷新传出的缓冲区?](/upload/InfoBanner/zyjiaocheng/702/da0cf11ee47a438fbe97e24937b1c25c.jpg)
我使用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 = StreamHandler
class StreamFactory(http.HTTPFactory):
protocol = StreamProtocol
if __name__ == '__main__':
from twisted.internet import reactor
reactor.listenTCP(8800, StreamFactory())
reactor.run()
解决方法:
使用time.sleep()可防止扭曲完成其工作.为了使它工作,你不能使用time.sleep(),你必须将控制返回到扭曲状态.修改现有代码的最简单方法是使用twisted.internet.defer.inlineCallbacks,这是切片面包以来的最佳选择:
#!/usr/bin/env python
import time
from twisted.web import http
from twisted.internet import protocol
from twisted.internet import reactor
from twisted.internet import defer
def wait(seconds, result=None):
"""Returns a deferred that will be fired later"""
d = defer.Deferred()
reactor.callLater(seconds, d.callback, result)
return d
class StreamHandler(http.Request):
BOUNDARY = 'BOUNDARY'
def writeBoundary(self):
self.write("--%s\n" % (self.BOUNDARY))
def writeStop(self):
self.write("--%s--\n" % (self.BOUNDARY))
@defer.inlineCallbacks
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()
yield wait(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()
yield wait(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 = StreamHandler
class StreamFactory(http.HTTPFactory):
protocol = StreamProtocol
if __name__ == '__main__':
reactor.listenTCP(8800, StreamFactory())
reactor.run()
这适用于Firefox,我想它正确回答了你的问题.
内容总结
以上是互联网集市为您收集整理的python – 使用Twisted的twisted.web类,如何刷新传出的缓冲区?全部内容,希望文章能够帮你解决python – 使用Twisted的twisted.web类,如何刷新传出的缓冲区?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。