import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options

'''
[server] python hello.py

[client] curl http://localhost:8000/?greeting=liubida
[client] curl http://localhost:8000/reverse/iamapig
'''

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        greeting = self.get_argument('greeting', 'Hello')
        self.write(greeting + ', user!\n')

    def write_error(self, status_code, **kwargs):
        self.write("Gosh darnit, user! You caused a %d error." % status_code)

class ReverseHandler(tornado.web.RequestHandler):
    def get(self, input):
        self.write(input[::-1] + '\n')

if __name__ == '__main__':
    tornado.options.parse_command_line()

    app = tornado.web.Application(
        handlers=[
            (r'/', IndexHandler),
            (r'/reverse/(\w+)', ReverseHandler),
        ]
    )
    httpserver = tornado.httpserver.HTTPServer(app)
    httpserver.listen(8000)

    tornado.ioloop.IOLoop.instance().start()

