python - tornado server serve a file -
i know how serve static files / pngs etc far kept under web static path.
how go serving file @ path, /usr/local/data/table.csv ?
also, wondering if display page wise results(pagination) more concerned serving arbitrary location files + stay if remove them local (i mean once uploaded / cached). [that separate ques though]
on basic level, need read file , write response:
import os.path 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) open(file_location) 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()
(disclaimer. quick writeup, won't work in cases, careful.)
Comments
Post a Comment