Skip to content

Instantly share code, notes, and snippets.

@faisal-w
Last active July 22, 2021 02:37
Show Gist options
  • Save faisal-w/44694c2620d6ed692221 to your computer and use it in GitHub Desktop.
Save faisal-w/44694c2620d6ed692221 to your computer and use it in GitHub Desktop.
A working example of how to upload file with Tornado Python.
import tornado.httpserver, tornado.ioloop, tornado.options, tornado.web, os.path, random, string
#Actually we didn't need this class
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", IndexHandler),
(r"/upload", UploadHandler)
]
tornado.web.Application.__init__(self, handlers)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("tornadoUpload.html")
class UploadHandler(tornado.web.RequestHandler):
def post(self):
file1 = self.request.files['file1'][0]
original_fname = file1['filename']
output_file = open("uploads/" + original_fname, 'wb')
output_file.write(file1['body'])
self.finish("file " + original_fname + " is uploaded")
settings = {
'template_path': 'templates',
'static_path': 'static',
"xsrf_cookies": False
}
application = tornado.web.Application([
(r"/", IndexHandler),
(r"/upload", UploadHandler)
], debug=True,**settings)
print "Server started."
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment