Content-Length header not returned from Pylons response

大城市里の小女人 提交于 2019-12-11 00:56:10

问题


I'm still struggling to Stream a file to the HTTP response in Pylons. In addition to the original problem, I'm finding that I cannot return the Content-Length header, so that for large files the client cannot estimate how long the download will take. I've tried

response.content_length = 12345

and I've tried

response.headers['Content-Length'] = 12345

In both cases the HTTP response (viewed in Fiddler) simply does not contain the Content-Length header. How do I get Pylons to return this header?

(Oh, and if you have any ideas on making it stream the file please reply to the original question - I'm all out of ideas there.)

Edit: while not a generic solution, for serving static files FileApp allows sending the Content-Length header. For dynamic content it looks like Alex Martelli's answer is the only option.


回答1:


There's a bit of middleware code here that ensures all responses get a content length header if they're missing it. You could tweak it so that you set some other header in your response (say 'X-The-Content-Length') and the middleware uses that to make the content length if the latter's missing. I view the whole thing as a workaround for what I consider a pylons bug (its cavalier attitude to content length!) but apparently the pylons authors disagree with me on that score, so it's nice to at least have workarounds for it!-)




回答2:


Try:

response.headerlist.append((str("Content-Length"), str(" 123456")))


来源:https://stackoverflow.com/questions/2777866/content-length-header-not-returned-from-pylons-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!