我正在尝试将SMTP服务器连接到域名
import socket()
import smtplib
import dns.resolver
getdomain = user_email.split('@')
check_domain = dns.resolver.query(getdomain[1], 'MX')
mxrecords=check_domain[0].exchange
host=socket.gethostname()
server=SMTP()
server.connect(mxrecords)
这让我犯了个错误
if not port and (host.find(':') == host.rfind(':')):
AttributeError: 'Name' object has no attribute 'find'
但当我把mxrecords改成string时,它就工作了
mxrecords=str(check_domain[0].exchange)
有人能解释一下为什么它接受字符串吗?你知道吗
从https://docs.python.org/3/library/smtplib.html
您可以知道
connect()
需要一个字符串参数host
。你知道吗connect()
可以单击然后重定向到链接,然后您可以看到:在这里您可以知道param
host='localhost'
默认值是一个字符串。你知道吗编辑
我查了你的密码
印刷品
表示
mxrecords
对象是dns.name.Name
对象,而不是字符串。你知道吗如果单击
connect
方法的源代码,您会发现host
应该是一个字符串:在代码中可以找到
host.find(':') == host.rfind(':')
,它与您的错误相匹配。你知道吗检查
dns.name.Name
源代码,您会发现Name
类有一个to_text
方法:所以您应该使用
mxrecords.to_text()
来获取MX服务器名。你知道吗第三方
dns.resolver
包返回的对象不是标准库smtplib
所知道的类型(或类,如果有帮助的话)。你知道吗很多时候,在与库API接口时,作为程序员,您的任务是将一个API作为输出返回的自定义表示转换为另一个API调用的适合作为输入的不同表示。你知道吗
系统库需要特别注意这一点。如果
smtplib
(或者甚至socket
)知道您的特定解析器,那么与其他解析器一起使用会更加困难。即使您的解析器也是Python标准库的一部分,这样的内部依赖也会引入不受欢迎的僵化、内部耦合,以及潜在的一些讨厌的内部版本控制问题。你知道吗相关问题 更多 >
编程相关推荐