gpt4 book ai didi

python - 流期间管道破裂

转载 作者:太空狗 更新时间:2023-10-30 00:17:45 25 4
gpt4 key购买 nike

我有一个 Django 项目,我正在处理一些 mp3 文件。

我有同样的问题: Streaming mp3 files with django, read from a page with <audio>

让我解释一下:我想用 Django 和一个 <audio>流式传输一个 ogg在我的 html 页面中标记

我有一个类似 domain.tld/song/show/X/ 的网址, 其中X是我歌曲的ID。我可以使用 VLC 进行流式传输(直接使用文件路径),我可以在测试期间进行流式传输,(我写下我收到的内容并使用 VLC 进行读取)。

但是当我打开浏览器并加载主页时 domain.tld我在哪里和<\audio\>带有 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

我每次尝试流式传输时都会收到两次。


编辑 2005 年 29 月 15 日:

我按照 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 view 上的情况

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

关于python - 流期间管道破裂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16811849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com