import dns from dns import resolver # 获取主机A记录 ans=resolver.query('www.baidu.com','A') for i in ans.response.answer: for j in i.items: print j # 获取ns记录 my_resolver=dns.resolver.Resolver() my_resolver.nameservers = ['8.8.8.8'] # 指定dns服务器 ans=my_resolver.query('sd.gov.cn','NS') # 默认dns服务器 ans=resolver.query('sd.gov.cn','NS') for i in ans.response.answer: for j in i.items: print j # 获取mx记录 mx=resolver.query('163.com','MX') for k in mx: print k 不要将python脚本命名为dns.py