Explore Python's Tornado framework support for subdomains and pan domain names

Source: Internet
Author: User
Tags subdomain
In fact, Tornado sub-domain name and the pan domain name (in addition to special instructions, the following sub-domain name and the pan domain name are referred to as the pan domain name) is not new, two years ago I wrote with Tornado Open source site http://poweredsites.org have a pan domain name support, But Tornado's official document does not explicitly explain this function, although the source code is commented, the end is a bit obscure, this is not, recently mywaiting classmate encountered this problem, I was invited to special essays this blog, share my little experience on this.

In general, adding a URL Map routing table with Tornado is a direct way to handlers to application, such as the official Chatdemo:

Class Application (tornado.web.Application):  def __init__ (self):    handlers = [      (r "/", MainHandler),      ( R "/auth/login", Authloginhandler),      (R "/auth/logout", Authlogouthandler),      (R "/a/message/new", Messagenewhandler),      (R "/a/message/updates", Messageupdateshandler),    ]    settings = dict (      cookie_ Secret= "43oetzkxqagaydkl5gemgejjfuyh7eqnp2xdtp1o/vo=",      login_url= "/auth/login",      template_path= Os.path.join (Os.path.dirname (__file__), "Templates"),      Static_path=os.path.join (Os.path.dirname (__file__), " Static "),      xsrf_cookies=true,      autoescape=" Xhtml_escape ",    )    tornado.web.application.__init__ (Self, handlers, **settings)

This way is actually added is a domain name wildcard URL mapping table, that is, the domain name & sub-domain name, as long as the access can resolve to this chatdemo, "/auth/login" "/auth/login" these URLs will be able to run normally. Assuming that the www.feilong.me, abc.feilong.me, feilong2.me this three (sub) domain name are configured to be able to host by this Chatdemo program, then access to these three (sub) domain name can be used normally this chatdemo, Anyway, the domain name is irrelevant.

In fact, in this way its interior is implemented by this add_handlers in application (the original code is commented as follows):

  def add_handlers (self, Host_pattern, host_handlers): "" "    appends the given handlers to our handler list.     Note that host patterns is processed sequentially    in the order they were added, and only the first matching pattern i s    used. This means, all handlers for a given host must is    added in a single add_handlers call. ""    

But it is implicitly called this add_handlers, its key point is the first parameter Host_pattern (matching domain name) on the way, the default added Host_pattern is ". *$", that is, the domain name wildcard, to support the pan domain name, You only need to explicitly call add_handlers to add the appropriate host_pattern and handlers.

Next to the Poweredsites source to introduce tornado to the pan domain name support, app.py in the application inside there are such a few words:

Super (Application, self). __init__ (handlers, **settings)   # Add handlers for sub domains for  Sub_handler in Sub_ Handlers:    # Host pattern and handlers    self.add_handlers (Sub_handler[0], sub_handler[1])

The Common way super (application, self). __init__ (handlers, **settings) adds poweredsites for the root domain handlers, The handlers of the subdomain and the generic domain name are then explicitly added with a for loop. Here the sub_handlers of each sub-domain handlers, the last one is the handlers of the pan domain name:

Sub_handlers.append (site.sub_handlers) sub_handlers.append (blog.sub_handlers) sub_handlers.append (admin.sub_ Handlers) # wildcard subdomain handler for project should being the last One.sub_handlers.append (project.sub_handlers)

The Sub_handlers (site.sub_handlers) of the specified subdomain is this way, and the first element here is Host_pattern:

Sub_handlers = ["^sites.poweredsites.org$",        [         (R "/", _websiteindexhandler),         (R "/feeds", _ Websitesfeedshandler),         (r "/([a-z0-9]{32})", _websitehandler),         (r "/([^/]+)", Websitehandler),         ]        ]

The difference between a pan domain name (project.sub_handlers) is that the first element, which is used to do Host_pattern, is to include some subdomains:

Sub_handlers = ["^[a-za-z_\-0-9]*\.poweredsites.org$",        [(R "/", Projectindexhandler),         (R "/top", Projecttophandler),         (R "/opensource", Projectopensourcehandler),         ]        

In the use of the projectindexhandler of the pan domain name, the runtime specific sub-domain can be obtained by the following way:

Class Projectindexhandler (Projectbasehandler):  def get (self):    subdomain = Self.request.host.split (".") [0]

It should be noted that the URL mapping table inside the Tornado and Django is the same order, that is, the URL in order from top to bottom matching, as long as the match to the immediate end, no longer match, and the URL of the domain name and the generic domain name is higher than the priority of the wildcard domain name ". *$" ( This does not bother you, add_handlers will automatically do this for you). Similarly, for the pan domain name, because its sub-domain name is a wildcard, so the designation sub-domain name handlers need to be added before the pan domain name, such as admin, blog such sub-domain name handlers to put in the pan domain name, this is Poweredsites sub_ Handlers.append (project.sub_handlers) Put the reason for the last one, project this is the corresponding generic domain name, http://tornado.poweredsites.org is to rely on this one to achieve.

Note: Need to support the pan domain name, first of all, your domain name resolution to support the pan domain name.

Reprint Please specify source: Http://feilong.me/2012/08/wildcard-subdomain-support-in-tornado

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.