Tôi đang cố phát trực tuyến tệp video bằng cherrypy. Khi tôi truy cập localhost: 8080/stream? Video = video.avi nó bắt đầu tải xuống, nhưng sau một vài giây, nó chỉ "hoàn tất" quá trình tải xuống cho dù tệp có lớn đến mức nào. Tôi khá mới với điều này và không thể tìm ra lý do tại sao nó đang làm điều đó. Ngoài ra, tại sao nó thậm chí không nhận ra các tập tin nếu nó là Matroska (.mkv)?Truyền tệp bằng cherrypy
Đây là lớp Suối tôi:
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}
Làm điều tương tự. Cũng nhận được một loại dừng khi đệm và cũng không chắc chắn làm thế nào để tiếp tục tải xuống nguồn sau khi kích thước bộ đệm đã được đáp ứng. – Twisty