Serving mov file with Django

47 views
Skip to first unread message

Hanley Hansen

unread,
Dec 27, 2014, 11:00:08 AM12/27/14
to django...@googlegroups.com
I want to serve an mov file on the file system with Django the way Apache would. IBe managed to serve the file as a download but I'm looking to stream it so it plays in browser. I want to avoid setting up a alias in Apache. Though it's a static file I need to stream it securely based on the user. Is that possible?

Tim Chase

unread,
Dec 27, 2014, 11:54:15 AM12/27/14
to django...@googlegroups.com, hanley...@gmail.com
It sounds like you want to investigate the "Django sendfile"

https://www.google.com/search?q=django+sendfile

which makes use of headers which tell the wrapping server (Apache,
lighttpd, nginx, etc) to send a file straight from the file-system
without needing to keep your Django app in the loop. The request hits
your Django app, you do whatever auth check you need, and if it
passes, you use sendfile in/as your response to instruct your
web-server to send the corresponding file. There's a nice write-up at

http://www.sensibledevelopment.com/2010/11/django-sendfile-an-for-abstraction-large-file-serving-in-django/

that shows how it's used. I believe it has the limitation that the
file has to come from the same server rather than a media-server, but
it does ease the load that serving directly from Django would cause.

-tkc



Hanley Hansen

unread,
Dec 27, 2014, 1:29:42 PM12/27/14
to django...@googlegroups.com, hanley...@gmail.com, django...@tim.thechases.com
That makes sense. I've tried using sendfile but i'm not getting the behavior I expect.

I'm using the simple backend:

SENDFILE_BACKEND = 'sendfile.backends.xsendfile'

And i'm sending the file like this:

project_path = os.path.dirname(__file__)
path = os.path.join(project_path, "..", "songs", song.file_name)
return sendfile(request, path, attachment=True)

But that's resulting in a file download in the browser which is not the behavior I get when i add an alias to apache and serve it that way.

Tim Chase

unread,
Dec 27, 2014, 2:09:15 PM12/27/14
to Hanley Hansen, django...@googlegroups.com, django...@tim.thechases.com
On 2014-12-27 10:29, Hanley Hansen wrote:
> That makes sense. I've tried using sendfile but i'm not getting the
> behavior I expect.
>
> return sendfile(request, path, attachment=True)

Does it do what you want if you remove the "attachment=True"?

-tkc



Hanley Hansen

unread,
Dec 27, 2014, 2:15:10 PM12/27/14
to django...@googlegroups.com, hanley...@gmail.com, django...@tim.thechases.com
No it does the same thing.

Tim Chase

unread,
Dec 27, 2014, 5:55:30 PM12/27/14
to Hanley Hansen, django...@googlegroups.com, django...@tim.thechases.com
On 2014-12-27 10:29, Hanley Hansen wrote:
> I'm using the simple backend:
>
> SENDFILE_BACKEND = 'sendfile.backends.xsendfile'

Your initial email mentions running Apache. Are you seeing this on
an Apache server, or are you seeing it on the development server?
Also, how are you connecting Django to Apache? With mod_wsgi or some
other method?

You'd want to use the corresponding back-end for whichever deployment
environment you're using. Thus, if you're using Apache+mod_wsgi, you'd
want to use

SENDFILE_BACKEND = 'sendfile.backends.mod_wsgi'

If you have Apache+mod_xsendfile or lighttpd, you'd use the setting
you have (which suggests that you don't have mod_xsendfile enabled).

-tkc


Reply all
Reply to author
Forward
0 new messages