MX Record lookup and check

一笑奈何 提交于 2019-12-03 02:35:26

With dnspython module (not built-in, you must pip install it):

>>> import dns.resolver
>>> domain = 'hotmail.com'
>>> for x in dns.resolver.query(domain, 'MX'):
...     print(x.to_text())
...
5 mx3.hotmail.com.
5 mx4.hotmail.com.
5 mx1.hotmail.com.
5 mx2.hotmail.com.

[update]

For python 3 it is pip install dnspython3.

Take a look at dnspython, a module that should do the lookups for you just fine without needing to resort to system calls.

Why not use nslookup? This code should be compatible with 2.6+

import os
import re

__query = 'nslookup -q=mx {0}'
__pattern = '\*\*\sserver\scan\'t\sfind'

def check_for_mx_record(domain):
    try:
        command = __query.format(domain)
        with os.popen(command) as response:
            result = response.readlines()
            return all(re.match(__pattern,l) == None for l in result)
    except Exception:
        return False
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!