【问题标题】:Is there a way to change the root URL in python's Tornado web framework?有没有办法改变 python 的 Tornado web 框架中的根 URL?
【发布时间】:2017-12-09 11:06:10
【问题描述】:

我有一个 Tornado 网络服务器,上面运行着一个应用程序,这样当我去:

本地主机:8888/

我看到了我的应用程序的主页。例如,当我去时,

  • localhost:8888/static/image/logo.png
  • localhost:8888/about
  • 本地主机:8888/联系人
  • 等等..

我也得到了那些相关的物品。

我的问题是,有没有办法更改根位置,以便所有这些 URL 将 URL 的第一部分替换为:

  • localhost:8888/myApplication/
  • localhost:8888/myApplication/static/image/logo.png
  • 等等……

对不起,如果这是一个简单的问题!似乎找不到答案。

请注意,我想要一个解决方案,而不是手动更改所有页面路由正则表达式以包含该前缀。

【问题讨论】:

  • 您能否提供一些代码,您如何定义路由/处理程序?你用的是 Tornado 的模板,你用的是static_urlreverse_url吗?
  • 您可以阅读tornado.web document 发送static_url_prefix 获取服务器静态文件。
  • Tornado 没有提供任何方法来做到这一点。但是,我可能会问,手动更改网址有什么问题?您所要做的就是使用文本编辑器的查找/替换功能。
  • 我希望 Tornado 能提供一种方法。问题是我在另一台服务器上代理 Web 应用程序,地址如下:proxy-server/MyApplication 但是 Tornado 对静态 URL 感到困惑,认为它们应该位于 proxy-server/static/etc.. 而不是 proxy-server/MyApplication /static/etc... 所以我最终破解了 tornado 中的静态 url 函数来为我的应用程序字符串添加前缀。不是最好的解决方案,但是...
  • 我相信我们正在寻找相当于 Django SUB_SITE 的 Tornado。这是因为如果我们在连接到不同挂载点的代理或 wsgiadapter 模式下在同一主机上托管多个 tornado.web.Applications,我们希望应用程序可以在任何挂载点上进行配置,而无需更改所有路由。 (顺便说一句,Flask/Werkzeug 路由装饰器似乎没有遇到这个问题,但也许他们在路由匹配时使用re.search 而不是re.match...)。

标签: python tornado


【解决方案1】:

如果您使用tornado.web Web 框架,这些根 URL 将作为正则表达式存储在 Web 应用程序对象中。因此,实现这项工作的一种“hacky”方法是更改​​正则表达式。

假设您的网络应用程序设置为

my_application = tornado.web.Application([(r"/", my_handler), (r"/about", about_handler),])

您可以在启动事件循环之前遍历处理程序并修改每个处理程序的正则表达式,如下所示:

for handler in my_application.handlers[0][1]:
    handler.regex = re.compile(handler.regex.pattern.replace('/', '/myApplication/', 1))

【讨论】:

    【解决方案2】:

    如果您使用的是 Tornado 4.5+,我认为您在猴子补丁tornado.routing.PathMatches 中也有两种选择。

    1. tornado.routing.PathMatches.__init__():

      在这里,您可以在原始模式前面加上 r/\w* 之类的东西。如果您还想将原始正则表达式模式修改为在右侧开放(因为original version 始终确保$ 位于正则表达式模式的末尾),这可能会带来额外的好处。这将确保通过路径匹配进行路由将遵循 Apache RewriteRule 或 Django 路由器样式语义(如果您需要更具体的路径匹配控制,您需要显式匹配 ^$

      import re
      import tornado.routing
      from tornado.util import basestring_type
      
      def pathmatches_init(self, path_pattern):
      
          if isinstance(path_pattern, basestring_type):
      
              # restore path regex behavior to RewriteRule semantics
      
              # if not path_pattern.endswith('$'):
              #    path_pattern += '$'
              # self.regex = re.compile(path_pattern)
      
              if not path_pattern.startswith('^'):
                  path_pattern = r'/\w*' + path_pattern
      
              self.regex = re.compile(path_pattern)
          else:
              self.regex = path_pattern
      
          assert len(self.regex.groupindex) in (0, self.regex.groups), \
              ("groups in url regexes must either be all named or all "
               "positional: %r" % self.regex.pattern)
      
          self._path, self._group_count = self._find_groups()
      
      tornado.routing.PathMatches.__init__ = pathmatches_init
      
    2. tornado.routing.PathMatches.match()

      不是调用仅在路径开头匹配的re.match(),而是调用re.search(),因为它将搜索整个request.path 以查找匹配项,因此匹配任何URI 前缀:

      import tornado.routing
      
      def pathmatches_match(self, request):
          # change match to search
          match = self.regex.search(request.path)
          if match is None:
              return None
          if not self.regex.groups:
              return {}
      
          path_args, path_kwargs = [], {}
      
          # Pass matched groups to the handler.  Since
          # match.groups() includes both named and
          # unnamed groups, we want to use either groups
          # or groupdict but not both.
          if self.regex.groupindex:
              path_kwargs = dict(
                  (str(k), _unquote_or_none(v))
                  for (k, v) in match.groupdict().items())
          else:
              path_args = [_unquote_or_none(s) for s in match.groups()]
      
          return dict(path_args=path_args, path_kwargs=path_kwargs)
      
      tornado.routing.PathMatches.match = pathmatches_match
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 2019-06-08
      • 2021-02-27
      相关资源
      最近更新 更多