2010-11-26 6 views
1

cherrypy를 서버로 사용하고 있습니다. 이 서버는 .mp3 파일을 다운로드 할 수있는 기능을 제공합니다. .mp3 파일을 다운로드 할 수 있도록 다음 코드를 사용하고 있습니다. 문제는 내가 다운로드 한 후에 얻는 mp3 파일이 실제로 mp3 파일이어야하는 데이터 파일이라는 것입니다.cherrypy에서 mp3 파일 제공

import glob 
import os.path 
import cherrypy 
from cherrypy.lib.static import serve_file 

class Root: 
    def index(self, directory="."): 

     html = """<html><body><h2>Here are the files in the selected directory:</h2> 
     <a href="index?directory=%s">Up</a><br /> 
     """ % os.path.dirname(os.path.abspath(directory)) 
     for filename in glob.glob(directory + '/*'): 
      absPath = os.path.abspath(filename) 
      if os.path.isdir(absPath): 
       html += '<a href="/index?directory=' + absPath + '">' + os.path.basename(filename) + "</a> <br />" 
      else: 
       html += '<a href="/download/?filepath=' + absPath + '">' + os.path.basename(filename) + "</a> <br />" 

     html += """</body></html>""" 
     return html 
    index.exposed = True 

class Download: 

    def index(self, filepath): 
     return serve_file(filepath, "audio/mpeg", "attachment") 
    index.exposed = True 

if __name__ == '__main__': 
    root = Root() 
    root.download = Download() 
    cherrypy.quickstart(root) 

답변

3

mp3 파일이 포함 된 디렉토리를 정적 디렉토리로 설정해보십시오.

<a href="directory_with_mp3s/somemp3.mp3">some mp3</a> 
+0

감사를 : 이것은 당신이 다운로드 클래스의 없애 간단하게 다음과 같이 html로에서 MP3 파일에 대한 링크를 만들 수

'/directory_with_mp3s': { 'tools.staticdir.on': True, 'tools.staticdir.dir': 'directory_with_mp3s' } 

: 귀하의 cherrypy의 conf는 다음과 같이 포함해야 당신은 대답 James를 위해서. 나는 같은 방식으로 문제를 해결했지만,이 스레드를 거의 동일하게 업데이트하는 것을 잊어 버렸다. 그래도 답을 고맙게 생각합니다. – chochim

+0

문제 없습니다. 이것이 정답이라고 생각하면 미래의 문제를 놓치지 않는 사람은 누구나 알고있을 수 있습니다. – jamesaharvey