简体   繁体   English

Python Tornado请求处理程序映射

[英]Python Tornado Request Handler Mapping

I'm just getting started with Tornado and I was wondering how i can define a mapping so that all requests like below are are handled by a single handler. 我刚开始使用Tornado,我想知道如何定义一个映射,以便像下面这样的所有请求都由一个处理程序处理。

  1. /products/list /产品/列表
  2. /products/find/123 / products / find / 123
  3. /products/copy/123 / products / copy / 123
  4. /products/{action}/{argument1}/{argument2}/{argument3} / products / {action} / {argument1} / {argument2} / {argument3}

     class Application(tornado.web.Application): def __init__(self): handlers = [ (r"/", home.HomeHandler), (r"/products/", product.ProductHandler)] class ProductHandler(base.BaseHandler): def get(self, action, *args): self.write("Action:" + action + "<br>") for arg in args: self.write("argument:" + arg + "<br>") 

You aren't limited to listing a RequestHandler just once in the url matching, so you can do one of two things: Add a pattern explicitly matching each of the patterns you mention above like so: 您不仅限于在url匹配中仅列出一次RequestHandler,所以您可以做以下两件事之一:添加一个明确匹配上面提到的每个模式的模式,如下所示:

def __init__(self):
    handlers = [
        (r"/", home.HomeHandler),
        (r"/products/list/([0-9]+)", product.ProductHandler)
        (r"/products/find/([0-9]+)", product.ProductHandler)
        (r"/products/copy/([0-9]+)", product.ProductHandler)
        (r"/products/(\w+)/(\w+)/(\w+)", product.ProductHandler)]

Or you could say that "any URL that begins with "products" should be sent to the product handler," like so: 或者,您可以说“任何以“产品”开头的URL应该发送给产品处理程序,例如:

def __init__(self):
    handlers = [
        (r"/", home.HomeHandler),
        (r"/products/list/(.*)", product.ProductHandler)

and parse the variable list yourself in the ProductHandler. 然后自己在ProductHandler中解析变量列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM