1. 如何流传输大文件

问题?解法要流传输大文件,需要添加传输译码(Transfer-Encoding)区块头,这样才能一边下载一边显示。否则,浏览器将缓冲所有数据直到下载完毕才显示。)例子# Simple streaming server demonstration # Uses time.sleep to emulate a large file read import web import time urls = ( "/", "count_holder", "/(.*)", "count_down", ) app = web.application(urls, globals()) class count_down: def GET(self,count): # These headers make it work in browsers web.header('Content-type','text/html') web.header('Transfer-Encoding','chunked') yield 'Prepare for Launch!' j = 'Liftoff in %s...' yield '' count = int(count) for i in range(count,0,-1): out = j % i time.sleep(1) yield out yield '' time.sleep(1) yield 'Lift off' class count_holder: def GET(self): web.header('Content-type','text/html') web.header('Transfer-Encoding','chunked') boxes = 4 delay = 3 countdown = 10 for i in range(boxes): output = '' % (countdown - i) yield output time.sleep(delay) if __name__ == "__main__": app.run()