import tornado.ioloopimport tornado.webclass BaseHandler(tornado.web.RequestHandler):def get(self):self.write("Hello, world")application = tornado.web.Application([(r"/", BaseHandler),])if __name__ == "__main__":application.listen(8080)tornado.ioloop.IOLoop.instance.start()
File Uploads
Note: The file sits in RAM
the use of nginx's upload module is suggested
class ArgumentHandler(tornado.RequestHandler):def get(self):#name is requiredname = self.get_argument('name')names = self.get_arguments('names') # empty list if not presentself.write('Hello, {0}<br />'.format(name))if len(names) > 0:self.write('Your list of names are: {0}'.format(', '.join(names)))else:self.write("Provide a list of names in the 'names' argument!")
application = tornado.web.Application([ (r"/", MainHandler),(r"/hello/world/", HelloWorldHandler),(r"/hello/([^/]+)/?"), HelloAnythingHandler),])
class HelloWorldHandler(tornado.web.RequestHandler):def get(self):self.write("Hello, World")
Yes, I could drop the hello world handler and have the same functionality.class HelloAnythingHandler(tornado.web.RequestHandler):def get(self, anything):self.write("Hello, {0}".format(anything))
Let's see a working demo of a service!
Source code available at:
https://github.com/mattgen88/tornado-demo