index.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #coding=utf-8
  2. import os
  3. import io
  4. from PIL import Image
  5. from HTMLParser import HTMLParser
  6. import tornado.httpserver
  7. import tornado.ioloop
  8. import tornado.options
  9. import tornado.web
  10. from tornado.options import define, options
  11. define("port", default=8000, help="run on the given port", type=int)
  12. PATH=os.path.dirname(os.path.abspath(__file__))
  13. class MyHTMLParser(HTMLParser):
  14. def __init__(self):
  15. self.reset()
  16. self.ttype = "p"
  17. self.fed = []
  18. def handle_starttag(self, tag, attrs):
  19. self.ttype = str(tag)
  20. def handle_endtag(self, tag):
  21. self.ttype = "p"
  22. def handle_data(self, data):
  23. self.fed.append({"type": self.ttype, "text": data.strip()})
  24. def get_data(self):
  25. return self.fed
  26. class Application(tornado.web.Application):
  27. def __init__(self):
  28. handlers = [
  29. (r"/", MainHandler),
  30. (r"/img", ImageHandler),
  31. (r"/download", GenFileStreamerHandler),
  32. ]
  33. settings = dict(
  34. template_path=os.path.join(os.path.dirname(__file__), "templates"),
  35. static_path=os.path.join(os.path.dirname(__file__), "static"),
  36. debug=True,
  37. )
  38. tornado.web.Application.__init__(self, handlers, **settings)
  39. class MainHandler(tornado.web.RequestHandler):
  40. def get(self):
  41. parser = MyHTMLParser()
  42. parser.feed("<i>Xenomorphia resurrecta</i>, male; well-preserved, head hollow")
  43. self.render(
  44. "index.html",
  45. title = """Preservation of three-dimensional anatomy in phosphatized
  46. fossil arthropods enriches evolutionary inference""",
  47. author = """Achim H Schwermann, Tomy dos Santos Rolo, Michael S Caterino,
  48. Günter Bechly, Heiko Schmied, Tilo Baumbach, Thomas van de Kamp""",
  49. institute = """Steinmann Institute for Geology, Mineralogy and Paleontology,
  50. University of Bonn, Bonn, Germany; ANKA/Institute for Photon Science and
  51. Synchrotron Radiation, Karlsruhe Institute of Technology, Eggenstein-Leopoldshafen,
  52. Germany; Department of Agricultural and Environmental Sciences, Clemson University,
  53. Clemson, United States; State Museum of Natural History Stuttgart, Stuttgart, Germany;
  54. Institute of Crop Science and Resource Conservation, University of Bonn, Bonn, Germany;
  55. Laboratory for Applications of Synchrotron Radiation, Karlsruhe Institute of Technology,
  56. Karlsruhe, Germany""",
  57. data_list = [
  58. "NRM-PZ_Ar65716",
  59. "Saab",
  60. "Mercedes",
  61. "Audi"],
  62. desc = parser.get_data(),
  63. data_size = "1.3 GB"
  64. )
  65. class GenFileStreamerHandler(tornado.web.RequestHandler):
  66. CHUNK_SIZE = 512000
  67. @tornado.web.asynchronous
  68. @tornado.gen.engine
  69. def get(self):
  70. #print self.get_arguments("did")
  71. self.path = PATH + '/data/01_NRM-PZ_Ar65716/test.zip'
  72. file_name = "01_NRM-PZ_Ar65716.zip"
  73. self.set_header('Content-Type',
  74. 'application/octet-stream')
  75. self.set_header('Content-Disposition',
  76. 'attachment; filename=' + file_name)
  77. self.set_header("Content-Length",
  78. os.path.getsize(self.path))
  79. self.flush()
  80. fd = open(self.path, "rb")
  81. data = fd.read(self.CHUNK_SIZE)
  82. while data:
  83. self.write(data)
  84. yield tornado.gen.Task(self.flush)
  85. data = fd.read(self.CHUNK_SIZE)
  86. fd.close()
  87. self.finish()
  88. class ImageHandler(tornado.web.RequestHandler):
  89. CHUNK_SIZE = 512000
  90. @tornado.web.asynchronous
  91. @tornado.gen.engine
  92. def get(self):
  93. #print self.get_arguments("did")
  94. self.path = PATH + '/static/data/1.png'
  95. self.set_header('Content-Type', 'image/png')
  96. self.set_header("Content-Length",
  97. os.path.getsize(self.path))
  98. self.flush()
  99. fd = open(self.path, "rb")
  100. data = fd.read(self.CHUNK_SIZE)
  101. while data:
  102. self.write(data)
  103. yield tornado.gen.Task(self.flush)
  104. data = fd.read(self.CHUNK_SIZE)
  105. fd.close()
  106. self.finish()
  107. if __name__ == "__main__":
  108. tornado.options.parse_command_line()
  109. http_server = tornado.httpserver.HTTPServer(Application())
  110. http_server.listen(options.port)
  111. tornado.ioloop.IOLoop.instance().start()