Guest User

Untitled

a guest
Jan 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import tornado.web
  2. import tornado.ioloop
  3. import logging
  4. import os.path
  5.  
  6.  
  7. def clean_filename(filename):
  8. i = filename.rfind(".")
  9. if i != -1:
  10. filename = filename[0:i] + filename[i:].lower()
  11. return filename
  12.  
  13.  
  14. def get_or_create_file(chunk, dst):
  15. if chunk == 0:
  16. f = file(dst, 'wb')
  17. else:
  18. f = file(dst, 'ab')
  19. return f
  20.  
  21.  
  22. class UploadHandler(tornado.web.RequestHandler):
  23. def post(self):
  24. filename = clean_filename(self.get_argument('name'))
  25. dst = os.path.join('static', 'upload', filename)
  26.  
  27. chunk = int(self.get_argument('chunk', '0'))
  28. chunks = int(self.get_argument('chunks', 0))
  29.  
  30. f = get_or_create_file(chunk, dst)
  31. body = self.request.files['file'][0]['body']
  32. f.write(body)
  33. f.close()
  34.  
  35. self.write('uploaded')
  36.  
  37.  
  38. if __name__ == "__main__":
  39. logging.basicConfig()
  40.  
  41. handlers = [
  42. (r"/upload", UploadHandler),
  43. ]
  44.  
  45. application = tornado.web.Application(handlers,
  46. static_path='static',
  47. )
  48. application.listen(80)
  49. tornado.ioloop.IOLoop.instance().start()
Add Comment
Please, Sign In to add comment