2010-11-26 11 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には、次のようなものが含まれている必要がありますあなたはジェームスに答えてください。私は同じ方法で問題を解決しましたが、同じことについてこのスレッドを更新するのを忘れました。それでも、あなたの答えをありがとう。 – chochim

+0

問題ありません。これが正解であると感じる場合は、それをそのようにマークしておきたいので、将来この質問につきまとう者は誰でも知ることができます。 – jamesaharvey

関連する問題