龙卷风中的静态文件编码或其他错误

时间:2013-07-11 14:23:50

标签: python static tornado

我需要龙卷风中的静态文件(html,scryptes,pictures,css)的帮助。 Standart文件处理程序没用,但是请求url一定不能包含静态前缀。 服务器用于移动应用程序项目。

代码:

application = tornado.web.Application([
    (r"/(.*)", static),
])


class static(tornado.web.RequestHandler):
def get(self, url):
    print 'static', url
    try:
        data = open(r'static/'+url,'rb').read()
        print 'file found', url
    except:
        data = 'error. file not found'
        print 'file not found', url
    self.write(data)

尝试获取图片失败。浏览器显示不同的字符 显示了html页面,但似乎css加载了故障。

有什么方法可以做到吗?

python 2.7,windows 7 x64(仅用于测试)。

问题解决了:

    (r"/(.*)", tornado.web.StaticFileHandler, {"path": r"C:\Python27\***\static"}),

1 个答案:

答案 0 :(得分:0)

您可以使用Tornado的服务器静态文件的能力:

application = tornado.web.Application(
    [(r"/(.*)", static)],
    static_path="your path here"
)

但它意味着调用url: www.domain.com/static/anyfile.txt

编辑: 如果您想拥有不同的基本网址,可以添加参数: 创建应用程序时static_url_prefix="/toto/"

然后:www.domain.com/toto/anyfile.txt将提供您的文件