python-3.x 在__getattr__中引发属性错误(属性)属性错误:至文本(_T)

fumotvh3  于 2022-12-24  发布在  Python
关注(0)|答案(2)|浏览(134)

这个程序是假设返回域名和MX解析域。但它给这个错误,不知道我一直在做错了什么。我收到这个错误:* * getattr**引发属性错误(属性)属性错误:至文本(_T)

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import socket
import dns.resolver
import re

domain = ['gmail.com' , 'hotmail.com' , 'abcfder.abc']

#function for mx
def getmx (domain):
    nomx= "no.mx.resolved."
try:
    result = dns.resolver.query(domain, 'MX')
    #for mailexchange in result:
    return (domain, result.to_text())
    #break
except dns.exception.DNSException as e:
    if isinstance(e, dns.resolver.NXDOMAIN):
    return (domain, nomx)
    #break

#function to remove '\n' from list

def clean (domainname):
    tmpstr = domainname
    domainname = tmpstr.strip()
    return (domainname)

#loop to keep sending domain names to MX function till EOF
x=0
while x<len(domain):
    strtmp = domain[x]
    d = clean(strtmp)
    #print (d)
    mx=getmx(d)
    print (mx)
    x=x+1   
f.close()
xtupzzrd

xtupzzrd1#

查询返回一个没有to_text()方法的Answer对象,但应答有一个包含to_text()的响应属性,尝试:

result.response.to_text()
rqqzpn5f

rqqzpn5f2#

您需要对接收请求响应的变量执行for循环。例如:

answer = dns.resolver.resolve(domain, 'A')
for record in answer:
    print(record.to_text())

相关问题