如何测试域名是' www' python中的域名或子域名或名称服务器域或邮件域?

时间:2014-12-31 08:12:20

标签: python subdomain appdomain tld

我想要测试的域名列表是主域名或子域名还是名称服务器或邮件服务器:

  • www.domain.com
  • ns1.domain.com
  • mail.domain.com
  • community.domain.com
  • mx.domain.com
  • mx3.domain.com
  • aspmx.l.google.com
  • forums.domain.com

我使用了tldextract python库

    from tld import get_tld
    import tldextract

    class DomainOpt(object):
    """docstring for DomainOpt"""
    _domainname = ''
    _tldextract = None
    _domaintype = ''

        def __init__(self, domainname):
            super(DomainOpt, self).__init__()
            self._domainname = domainname
            self._tldextract = tldextract.extract("http://%s/" % self._domainname)
            if self._tldextract.subdomain == 'mail':
                self._domaintype = 'maildomain'
            elif self._tldextract.subdomain in ['ns1','ns2','ns3','ns4','ns5','ns6',
            'ns7','ns8','ns9','ns10','ns11','ns12','ns13','ns14', 'ns15', 'ns16']:
                self._domaintype = 'dnsdomain'
            elif self._tldextract.subdomain is not None:
                self._domaintype = 'subdomain'
            else:
                self._domaintype = 'domain'

        @property
        def domainname(self):
            return get_tld(str('http://%s/' % self._domainname), fail_silently=True)

        @property
        def domain(self):
            return self._tldextract.domain
        @property
        def subdomain(self):
            return self._tldextract.subdomain
        @property
        def suffix(self):
            return self._tldextract.suffix
        @property
        def domaintype(self):
            return self._domaintype

2 个答案:

答案 0 :(得分:1)

tldextract库与您需要的相反:给定一个字符串" mail.example.com",它将返回" com"而不是" mail"。为了做你需要的,你不需要任何图书馆;只需在域名中搜索"。"并取其前面的子串。

答案 1 :(得分:1)

试试这个:

hostList = [
    'www.domain.com',
    'ns1.domain.com',
    'mail.domain.com',
    'community.domain.com',
    'mx.domain.com',
    'mx3.domain.com',
    'aspmx.l.google.com',
    'forums.domain.com'
    ]

for h in hostList:
    splitHost = h.split('.')
    tld = splitHost.pop()
    dom = splitHost.pop()
    host = '.'.join(splitHost)
    print 'FQHN: : ', h
    print 'Host  : ', host
    print 'Domain: ', dom
    print 'TLD   : ', tld
    print

另请注意,这仅适用于简单的' TLD,如comnetorg