如何测试域在python中是“www”域还是子域或名称服务器域或邮件域?

2024-09-29 10:31:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有要测试的域列表,它们是主域还是子域,名称服务器还是邮件服务器:

  • 你知道吗www.domain.com你知道吗
  • ns1。域名.com你知道吗
  • 你知道吗mail.domain.com你知道吗
  • 你知道吗社区.domain.com你知道吗
  • 你知道吗mx.domain.com你知道吗
  • mx3。域名.com你知道吗
  • aspmx.l.公司。谷歌网站你知道吗
  • 你知道吗论坛.domain.com你知道吗

我使用了tldextractpython库

    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

Tags: subdomainself服务器comreturndomaindefproperty
2条回答

试试这个:

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

还要注意,这只适用于comnetorg等“简单”tld

tldextract库的作用与您需要的相反:给定一个字符串“mail.example.com,它将返回“com”而不是“mail”。要做您需要的事情,您不需要任何库;只需在域名中搜索“.”并获取它前面的子字符串。你知道吗

相关问题 更多 >