Issue #932 resolved

Function analogous to serve_file for open file objects

Anonymous created an issue

I asked the following question:

In there is a method: serve_file(path, content_type=None, disposition=None, name=None) where the argument "path" must be a real file (absolute path). Is there something similar to serve any Python file object?

and got the following answer from Alex Martelli:

From studying I'd have to say, no: the serve_file function is "monolithic", and does its own bodyfile = open(path, 'rb'), nor does there seem to be any alternative way. Pity, as it would be easy to refactor the function and add another e.g. serve_open_file to cover your case, having both delegate to an internal function for the "hard" logic such as multipart/byteranges serving. May be worth your while to open a feature request ("enhancement ticket") on -- may not be a killer feature but I can see use cases, and implementing it wouldn't be hard for the cherrypy people (visit their site and follow the instructions on the page to "log in" to it).

It would be really nice to have this function!

-- tsf

Reported by

Comments (3)

  1. visteya

    A `serve_file()` which accepts a file object won't be able to set Last-Modified, Content-Type, and Content-Length response headers.

    A `serve_file()` which accepts a file object has a problem: `serve_file()` performs a `seek()` on the file object if the request has a Range header.

    Since supporting ranges can't be done reliably, and that's about the only benefit left in `serve_file()` after you take away setting of Last-Modified, Content-Type and Content-Length, I think I'd be inclined to just explain to people that the solution for this problem is to return their open file object from the page handler, and set Content-Length and Content-Type themselves as appropriate.

  2. Log in to comment