выступающей mp3 файлы от cherrypy - PullRequest
1 голос
/ 26 ноября 2010

Я использую cherrypy в качестве сервера.Этот сервер дает вам возможность загружать файлы .mp3.Я использую следующий код для загрузки файлов .mp3.Проблема в том, что mp3-файл, который я получаю после загрузки, является файлом данных, который на самом деле должен быть 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)

1 Ответ

3 голосов
/ 01 декабря 2010

Попробуйте настроить каталог, содержащий mp3-файлы, в качестве статического каталога.Ваш вишневый конф должен содержать что-то вроде этого:

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

Это позволит вам избавиться от класса Download и просто создать ссылки на mp3-файлы в HTML, которые выглядят так:

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