title: Streaming mp3 files with django, read from a page with <audio>
body: <p>I'm trying to make a small app to stream my mp3 files with a web interface, and I thought about playing with  and do the server side in python with django.</p>
  
  <p>I want to have urls like /stream/ID to stream the mp3 corresponding to that ID.
  I made a view in django to serve these files using different methods, the last one I tried being the one described <a href="http://metalinguist.wordpress.com/2008/02/12/django-file-and-stream-serving-performance-gotcha/" rel="nofollow">here</a></p>
  
  <p>If I access /stream/ID from firefox, it plays the mp3 directly with firefox-totem or some plugin like that. If I use the page with my audio player with the same url as the source it doesn't work at all (works with a link to an mp3 file served by apache). </p>
  
  <p>here is the code of my view (only send one test file)  </p>
  
  <pre>def stream(request):  
      resp =  HttpResponse(FileIterWrapper(open('/.../test.mp3',"rb")),mimetype='audio/mpeg')  
      resp['Content-Length'] = os.path.getsize("/.../test.mp3")  
      resp['Content-Disposition'] = 'filename=test.mp3'  
      return resp</pre>
  
  <p>I cut the full path, it is not the problem.</p>
  
  <p>When looking at the django runserver output, I noticed that each time the audio player tries, I get these 2 errors, </p>
  
  <pre>Traceback (most recent call last):
    File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 281, in run
      self.finish_response()
    File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 321, in finish_response
      self.write(data)
    File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 417, in write
      self._write(data)
    File "/usr/lib/python2.6/socket.py", line 318, in write
      self.flush()
    File "/usr/lib/python2.6/socket.py", line 297, in flush
      self._sock.sendall(buffer(data, write_offset, buffer_size))
  error: [Errno 104] Connection reset by peer
  ----------------------------------------
  Exception happened during processing of request from ('127.0.0.1', 42891)
  Traceback (most recent call last):
    File "/usr/lib/python2.6/SocketServer.py", line 283, in _handle_request_noblock
      self.process_request(request, client_address)
    File "/usr/lib/python2.6/SocketServer.py", line 309, in process_request
      self.finish_request(request, client_address)
    File "/usr/lib/python2.6/SocketServer.py", line 322, in finish_request
      self.RequestHandlerClass(request, client_address, self)
    File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 562, in __init__
      BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
    File "/usr/lib/python2.6/SocketServer.py", line 618, in __init__
      self.finish()
    File "/usr/lib/python2.6/SocketServer.py", line 661, in finish
      self.wfile.flush()
    File "/usr/lib/python2.6/socket.py", line 297, in flush
      self._sock.sendall(buffer(data, write_offset, buffer_size))
  error: [Errno 32] Broken pipe
  </pre>
  
  <p>no problem/error when I acces the stream URL directly.</p>
  
  <p>I tried the same in chromium (latest version, with ffmpeg-extra installed), it works fine with the mp3 from apache, but timeouts when using the stream url.</p>
  
  <p>I tried setting different headers in the response, but with no success.
  I'm currently setting the content-length, content-type and content-disposition</p>
  
  <p>I'm looking for new ideas to try.</p>
  
  <p>Thanks for your help!</p>