views:

212

answers:

2

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  A: 

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!-)

Alex Martelli
Thanks, Alex, that bit of middleware works. I don't even need to modify it - since I can't get the "streaming" to work anyway calling `len(body)` returns the correct value. I'm not sure why such a simple thing requires a bit of "middleware", though.Congratulations on hitting 100K, by the way!
Evgeny
@evgeny, if you can convince the Pylons committers that content length _should_ always be returned, you'll earn everybody's gratitude including mine -- but it seems they're still convinced otherwise. Thanks for the congrats!-)
Alex Martelli
Well, never mind "always", but at least if I manually set it in the headers it should be returned!
Evgeny
A: 

Try:

response.headerlist.append((str("Content-Length"), str(" 123456")))
Peter McGrattan
No, that doesn't work, sorry.
Evgeny