Created
April 3, 2015 05:41
-
-
Save gurel/7e64828cb8e364b79ad5 to your computer and use it in GitHub Desktop.
Tornado - Routing - Pass fields as params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.web | |
import tornado.httpserver | |
class TestParamsHandler(tornado.web.RequestHandler): | |
def get(self, param1, param2, param3): | |
param2 = param2 if param2 else 'default2' | |
param3 = param3 if param3 else 'default3' | |
self.write( | |
{ | |
'param1': param1, | |
'param2': param2, | |
'param3': param3 | |
} | |
) | |
# My initial answer is above, but I think the following is better. | |
class TestParamsHandler2(tornado.web.RequestHandler): | |
def get(self, **params): | |
self.write(params) | |
application = tornado.web.Application([ | |
(r"/test1/(?P<param1>[^\/]+)/?(?P<param2>[^\/]+)?/?(?P<param3>[^\/]+)?", TestParamsHandler), | |
(r"/test2/(?P<param1>[^\/]+)/?(?P<param2>[^\/]+)?/?(?P<param3>[^\/]+)?", TestParamsHandler2) | |
]) | |
http_server = tornado.httpserver.HTTPServer(application) | |
http_server.listen(8080) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment