2016-11-05 24 views
0

私は静的ファイル/ pngsなどを提供する方法を知っています。竜巻サーバーはファイルを提供する

パスで/usr/local/data/table.csvというファイルを提供するにはどうすればよいですか?

また、ページワイズの結果(ページ番号)を表示できるかどうかは疑問でしたが、ローカルから削除しても、任意の場所ファイルを提供することに心配しています。最も基本的なレベルでは

答えて

0

[それはかかわらず、別のQUES可能性]、あなたがあなたのファイルを読み込み、応答にそれを記述する必要があります。

import os.path 
from mimetypes import guess_type 

import tornado.web 
import tornado.httpserver 

BASEDIR_NAME = os.path.dirname(__file__) 
BASEDIR_PATH = os.path.abspath(BASEDIR_NAME) 

FILES_ROOT = os.path.join(BASEDIR_PATH, 'files') 


class FileHandler(tornado.web.RequestHandler): 

    def get(self, path): 
     file_location = os.path.join(FILES_ROOT, path) 
     if not os.path.isfile(file_location): 
      raise tornado.web.HTTPError(status_code=404) 
     content_type, _ = guess_type(file_location) 
     self.add_header('Content-Type', content_type) 
     with open(file_location) as source_file: 
      self.write(source_file.read()) 

app = tornado.web.Application([ 
    tornado.web.url(r"/(.+)", FileHandler), 
]) 

http_server = tornado.httpserver.HTTPServer(app) 
http_server.listen(8080, address='localhost') 
tornado.ioloop.IOLoop.instance().start() 

(免責事項このクイック過去記事で、ほとんどそれ。確かにすべての場合に動作しませんので、注意してください。)

関連する問題