亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

探究Python的Tornado框架對子域名和泛域名的支持

系統 2151 0

其實Tornado對子域名和泛域名(除了特別說明外,以下子域名和泛域名均簡稱為泛域名)的支持并不是什么新鮮事,兩年多前我用Tornado寫的開源網站 http://poweredsites.org 就有了對泛域名的支持,但是Tornado的官方文檔里并沒有明確對此功能進行說明,雖然源代碼里是有注釋的,終是有點隱晦,這不,近日mywaiting同學就遇到了這個問題,我應邀特撰此博文,分享下我對此的一點點經驗。

通常,用Tornado添加url映射路由表是直接傳handlers給Application這種方式的,比如官方的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)


          

這種方式其實添加的是一個域名通配的url映射表,即域名&子域名不限,只要訪問能夠解析到這個chatdemo上,“/auth/login” “/auth/login”這些url就都能夠正常運行。假設www.feilong.me、abc.feilong.me、feilong2.me這個三個(子)域名均配置為可由這個chatdemo程序來host,那么訪問這三個(子)域名均可以正常使用這個chatdemo,總之域名是無關的。

實際上,這種方式它的內部是通過Application里的這個add_handlers來實現的(原碼注釋如下):

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


          

只不過它是隱式的調用這個add_handlers而已,其關鍵點就在于第一個參數host_pattern(匹配域名的)上,上面那種方式,默認添加的host_pattern是”.*$”,即域名通配,若要支持泛域名,只需要顯式的調用add_handlers來添加相應的host_pattern和handlers即可。

接下來就以poweredsites的源碼來介紹Tornado對泛域名的支持,app.py里的Application里面有這么幾句:

???

            
 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])

          

常見的方式super(Application, self).__init__(handlers, **settings)添加的是根域名poweredsites的handlers,接著用for循環顯式添加的是子域名和泛域名的handlers。這里的sub_handlers依次放有各子域名的handlers,其最后一個是泛域名的handlers:

            
sub_handlers.append(site.sub_handlers)
sub_handlers.append(blog.sub_handlers)
sub_handlers.append(admin.sub_handlers)
# wildcard subdomain handler for project should be the last one.
sub_handlers.append(project.sub_handlers)


          

指定的子域名的sub_handlers(site.sub_handlers)是這個樣子的,這里的第一個元素就是host_pattern:

            
sub_handlers = ["^sites.poweredsites.org$",
        [
         (r"/", _WebsiteIndexHandler),
         (r"/feeds", _WebsitesFeedsHandler),
         (r"/([a-z0-9]{32})", _WebsiteHandler),
         (r"/([^/]+)", WebsiteHandler),
         ]
        ]


          

泛域名(project.sub_handlers)的區別也就在于這第一個元素,即用來做host_pattern的是通配一些子域名的:

            
sub_handlers = ["^[a-zA-Z_\-0-9]*\.poweredsites.org$",
        [(r"/", ProjectIndexHandler),
         (r"/top", ProjectTopHandler),
         (r"/opensource", ProjectOpensourceHandler),
         ]
        ]


          

在用到了泛域名的ProjectIndexHandler里,運行時具體的子域名就可以通過下面這樣的方式獲得:

            
class ProjectIndexHandler(ProjectBaseHandler):
  def get(self):
    subdomain = self.request.host.split(".")[0]


          

需要說明的是,Tornado里面的url映射表和Django一樣是有順序的,即url依次序由上到下匹配,只要匹配到就立即結束,不再往下匹配,而帶子域名和泛域名的url路由其匹配優先級是要高于通配域名”.*$”的(這個不用你操心,add_handlers會自動為你做到這一點)。同樣的,對于泛域名,因為其子域名是通配的,因此指定子域名的handlers需要放到泛域名前添加,如admin、blog這類子域名的handlers要放在泛域名之前,這就是poweredsites里sub_handlers.append(project.sub_handlers)放到最后一條的原因,project這條是對應泛域名的,http://tornado.poweredsites.org 就是靠這一條來實現的。

備注:需要支持泛域名,首先要你的域名解析支持泛域名。

轉載請注明出處:http://feilong.me/2012/08/wildcard-subdomain-support-in-tornado


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 麻豆国产高清精品国在线 | xxxx日本在线播放免费不卡 | 亚洲欧美在线看 | 日本免费不卡在线一区二区三区 | 中文字幕亚洲一区 | 青草资源站 | 国产99区 | 国产在线视频你懂得 | 美女黄色免费在线观看 | 国产精品亚洲欧美日韩久久 | 一级免费看片 | 国产亚洲女在线精品 | 国产精品区一区二区三 | 久草久草久草 | 国产一区二区三区在线观看免费 | 日韩精品区| 91久久夜色精品国产九色 | 九九精品九九 | 欧美第3页 | 色激情综合| 91在线视频播放 | 最新午夜韩国理论片77网 | 伊人久久一本 | 精品99re66 | 97精品免费视频 | 狠狠的操你| 日韩高清在线播放不卡 | 97免费看| 久久激情综合色丁香 | 最新国产网站 | 国内精品日本久久久久影院 | 成人999 | 久久天天躁狠狠躁夜夜爽蜜月 | 国产成人精品亚洲日本在线 | 天天舔天天射天天干 | 四虎免费在线 | 国产免费69成人精品视频 | 日韩国产中文字幕 | 国产在线精品观看 | 日本aaaa级毛片在线看 | 亚洲欧美日韩在线中文一 |