2013-07-11 37 views
0

我需要在龍捲風中使用靜態文件(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) 

Atempt得到圖片失敗。瀏覽器顯示不同的字符 html頁面顯示,但它似乎CSS加載faled。

是否有辦法做到這一點?

python 2.7,windows 7 x64(僅用於測試)。

問題就迎刃而解了:

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

不'static'在'Application'定義見'tornado.web.StaticFileHandler'?你可以在你的靜態處理程序的URL前加上'r'/ static /(.*)''?http://stackoverflow.com/a/9637895/594589 – dm03514

+0

感謝您的鏈接。 – user1941407

+0

[如何處理Tornado中的文件的HTTP GET請求?](http://stackoverflow.com/questions/9531092/how-to-handle-a-http-get-request-toa-a-文件中,龍捲風) – dm03514

回答

0

你可以用龍捲風的能力,服務器的靜態文件:

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

,但它意味着調用的網址: www.domain.com/static/anyfile.txt

編輯: 如果你想要有不同的基本網址,您可以添加參數: static_url_prefix="/toto/"創建應用程序時

則:www.domain.com/toto/anyfile.txt將成爲您的文件