我有一个Django项目,我在那里工作,我想流一些mp3文件。
我有同样的问题:使用django流式传输mp3文件,从带有
的页面读取让我解释一下:我想让在Django中流式传输一个ogg,并在我的html页面
中添加一个<audio>
标签。我有一个类似domain.tld/song/show/X/
的url,其中X
是我的歌曲的id。我可以用VLC进行流(直接使用文件路径),我可以在测试期间进行流(我用VLC编写我收到的内容并读取它)。
但是当我打开浏览器并加载我的主页domain.tld
时,我有和<audio>
balise与url domain.tld/song/show/1/
,我得到一个大的破裂管道,好像我的客户端关闭了连接。
我在别人的帖子上读到,当他们把服务器投入生产时,一些问题得到了解决。所以我推我的应用程序在服务器上,使用apache,与django.wgsi
在djangoproject.com。
我在Debian 7上运行python 2.7.3, Django版本1.5。这里是我的代码:
的歌/views.py
def playAudioFile(request, pk):
f = get_stream_song(pk)# return a pipe from pipes.Template
l = f.read() # the file is an ogg get by pydub.com
f.close()
size_read = 550000
sr = size_read
while sr == size_read:
print "rep"
r = l[:size_read]
l=l[size_read:]
sr = len(r)
yield r
time.sleep(0.1)
#url : ~/song/show/X/
#@login_required
def show_song(request, pk):
return StreamingHttpResponse(playAudioFile(request, pk), mimetype='audio/ogg',)
在我的HTML中,我只有:
<audio controls height="100" width="100" preload="auto">
<source src="/.../song/show/1/" type="audio/ogg">
<embed height="50" width="100" src="/.../song/show/1/">
</audio>
错误如下:
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
self.write(data)
File "/usr/lib/python2.7/wsgiref/handlers.py", line 215, in write
self._write(data)
File "/usr/lib/python2.7/socket.py", line 324, in write
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 104] Connection reset by peer
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 46392)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/home/lumy/SPhoque/SonoPhoque/SoPhoque/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 150, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 704, in finish
self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
每次我尝试流式传输时,都会遇到这个问题。
编辑15h 29/05:
我按照rahan的建议做了:查看Firebug和Firefox调试器:
客户端执行:
GET 1 200 OK localhost:8000 537.1KB 4.71s
Headers
Response Headersview source
Date Wed, 29 May 2013 13:08:54 GMT
Server WSGIServer/0.1 Python/2.7.3
Content-Type audio/ogg
Request Headersview source
Host localhost:8000
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20100101 Firefox/10.0.12 Iceweasel/10.0.12
Accept audio/webm,audio/ogg,audio/wav,audio/*;q=0.9,application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5
Accept-Language en-us,en;q=0.5
Connection keep-alive
Range bytes=0-
Referer http://localhost:8000/
和细节说明所有文档的总大小为1 MB(来自缓存的526 KB)
可能是我越过你现有的解决方案,我有一个建议,mp3流使用nginx
/apache
服务器,这些天有解决方案被称为sendfile
,例如在你的情况下django视图
def send_file_header(server_type):
header = "X-Sendfile" if server_type == "apache" else "X-Accel-Redirect"
return header
@login_required
def show_song(request, pk):
res = HttpResponse()
path = "/path/to/secret/x.mp3"
response[send_file_header('nginx')] = path
response['Content-Type']= "application/octet-stream"
response['Content-Disposition'] = "attachment; filename="x.mp3""
return response