123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #coding=utf-8
- import os
- import io
- from PIL import Image
- from HTMLParser import HTMLParser
- import tornado.httpserver
- import tornado.ioloop
- import tornado.options
- import tornado.web
- from tornado.options import define, options
- define("port", default=8000, help="run on the given port", type=int)
- PATH=os.path.dirname(os.path.abspath(__file__))
- class MyHTMLParser(HTMLParser):
- def __init__(self):
- self.reset()
- self.ttype = "p"
- self.fed = []
-
- def handle_starttag(self, tag, attrs):
- self.ttype = str(tag)
- def handle_endtag(self, tag):
- self.ttype = "p"
- def handle_data(self, data):
- self.fed.append({"type": self.ttype, "text": data.strip()})
-
- def get_data(self):
- return self.fed
- class Application(tornado.web.Application):
- def __init__(self):
- handlers = [
- (r"/", MainHandler),
- (r"/img", ImageHandler),
- (r"/download", GenFileStreamerHandler),
- ]
- settings = dict(
- template_path=os.path.join(os.path.dirname(__file__), "templates"),
- static_path=os.path.join(os.path.dirname(__file__), "static"),
- debug=True,
- )
- tornado.web.Application.__init__(self, handlers, **settings)
- class MainHandler(tornado.web.RequestHandler):
- def get(self):
-
- parser = MyHTMLParser()
- parser.feed("<i>Xenomorphia resurrecta</i>, male; well-preserved, head hollow")
-
- self.render(
- "index.html",
- title = """Preservation of three-dimensional anatomy in phosphatized
- fossil arthropods enriches evolutionary inference""",
- author = """Achim H Schwermann, Tomy dos Santos Rolo, Michael S Caterino,
- Günter Bechly, Heiko Schmied, Tilo Baumbach, Thomas van de Kamp""",
- institute = """Steinmann Institute for Geology, Mineralogy and Paleontology,
- University of Bonn, Bonn, Germany; ANKA/Institute for Photon Science and
- Synchrotron Radiation, Karlsruhe Institute of Technology, Eggenstein-Leopoldshafen,
- Germany; Department of Agricultural and Environmental Sciences, Clemson University,
- Clemson, United States; State Museum of Natural History Stuttgart, Stuttgart, Germany;
- Institute of Crop Science and Resource Conservation, University of Bonn, Bonn, Germany;
- Laboratory for Applications of Synchrotron Radiation, Karlsruhe Institute of Technology,
- Karlsruhe, Germany""",
- data_list = [
- "NRM-PZ_Ar65716",
- "Saab",
- "Mercedes",
- "Audi"],
- desc = parser.get_data(),
- data_size = "1.3 GB"
- )
- class GenFileStreamerHandler(tornado.web.RequestHandler):
- CHUNK_SIZE = 512000
- @tornado.web.asynchronous
- @tornado.gen.engine
- def get(self):
- #print self.get_arguments("did")
- self.path = PATH + '/data/01_NRM-PZ_Ar65716/test.zip'
- file_name = "01_NRM-PZ_Ar65716.zip"
- self.set_header('Content-Type',
- 'application/octet-stream')
- self.set_header('Content-Disposition',
- 'attachment; filename=' + file_name)
- self.set_header("Content-Length",
- os.path.getsize(self.path))
- self.flush()
- fd = open(self.path, "rb")
- data = fd.read(self.CHUNK_SIZE)
- while data:
- self.write(data)
- yield tornado.gen.Task(self.flush)
- data = fd.read(self.CHUNK_SIZE)
- fd.close()
- self.finish()
- class ImageHandler(tornado.web.RequestHandler):
- CHUNK_SIZE = 512000
- @tornado.web.asynchronous
- @tornado.gen.engine
- def get(self):
- #print self.get_arguments("did")
- self.path = PATH + '/static/data/1.png'
- self.set_header('Content-Type', 'image/png')
- self.set_header("Content-Length",
- os.path.getsize(self.path))
- self.flush()
- fd = open(self.path, "rb")
- data = fd.read(self.CHUNK_SIZE)
- while data:
- self.write(data)
- yield tornado.gen.Task(self.flush)
- data = fd.read(self.CHUNK_SIZE)
- fd.close()
- self.finish()
- if __name__ == "__main__":
- tornado.options.parse_command_line()
- http_server = tornado.httpserver.HTTPServer(Application())
- http_server.listen(options.port)
- tornado.ioloop.IOLoop.instance().start()
|