【问题标题】:Custom whitelisting domain names - Python3自定义白名单域名 - Python3
【发布时间】:2017-10-05 13:49:40
【问题描述】:

在我的项目中,我想将一组域列入白名单以处理请求。它应该允许来自所列域、其子域和域上不同页面的所有请求。

因此,例如,如果列入白名单的域之一是 example.com,则它应该处理对 www.example.comabc.example.comhttps://abc.def.example.comexample.com/pg1 等的请求。

可用于此目的的最佳实用程序/库是哪个?或者,我需要编写自己的正则表达式吗?

【问题讨论】:

    标签: regex python-3.x whitelist domain-name


    【解决方案1】:

    您可以使用以下正则表达式来匹配域example.com 的子域。

    ^([a-zA-Z0-9]+\.)*example\.com\/?.*
    

    【讨论】:

    • 这也匹配.example.com。 :|
    • 现在修复它,所以它不会匹配.example.com
    【解决方案2】:

    您可以使用此 python 函数来检查是否应根据您的域允许 url:

    def isDomainAllowed(url)
      domain = 'example.com'
      match = re.search(r'example.com', url)
      if match and match.group() == domain:
        return True
      return False
    

    【讨论】:

    • 在这种情况下,它也会匹配kexample.com,这是一个完全不同的url。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多