python - Streaming files with cherrypy -
i trying stream video file using cherrypy. when go localhost:8080/stream?video=video.avi starts downloading, after few seconds "completes" download no matter how large file is. i'm rather new , cannot find out why doing that. also, why doesn't recognize file if matroska (.mkv) ?
here stream class:
class stream(object): @cherrypy.expose def default(self, video=none): base_path = ".." video = os.path.join(base_path, video) if video == none: return "no file specified!" if not os.path.exists(video): return "file not found!" f = open(video) size = os.path.getsize(video) mime = mimetypes.guess_type(video)[0] print(mime) cherrypy.response.headers["content-type"] = mime cherrypy.response.headers["content-disposition"] = 'attachment; filename="%s"' % os.path.basename(video) cherrypy.response.headers["content-length"] = size buf_size = 1024 * 5 def stream(): data = f.read(buf_size) while len(data) > 0: yield data data = f.read(buf_size) return stream() default._cp_config = {'response.stream': true}
i realised needed change open(video) open(video, 'rb') read file in binary mode. after file downloaded , worked.
Comments
Post a Comment