import tornado.ioloop
import tornado.web
from os import mkdir
from shutil import move
class MainHandler(tornado.web.RequestHandler):
def get(self):
items = ["Item 1", "Item 2", "Item 3"]
self.render("template.html", title="My title", items=items)
class FileUpHandler(tornado.web.RequestHandler):
def post(self):
# pull in details from our file that were created by nginx and show up
# as part of the request object like a normal POST parameter
fname = self.get_argument('upfile_name', 'uploaded.file')
content_type = self.get_argument('upfile_content_type', None)
file_path = self.get_argument('upfile_path', '')
file_size = self.get_argument('upfile_size', -1)
file_hash = self.get_argument('upfile_md5', '')
# create a directory that's pseudo random as a subdir of our static
# location so that we don't end up with a globbing nightmare on the up dir
#tmp_ = file_hash[:6]
dest_dir = "/tornado_tmp"
#mkdir(dest_dir)
# move the file into place
move(file_path, '%s/%s' % (dest_dir, fname))
# ... now render some type of page or do a redirect ...
self.redirect('/some/other/place/and/time')
application = tornado.web.Application([
(r"/", MainHandler),
(r"/upload", FileUpHandler),
])
if __name__ == "__main__":
print "Tornado has Started"
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()