gpt4 book ai didi

python - 为什么 mxrecord 应该改为字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:05 26 4
gpt4 key购买 nike

我正在尝试将 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 更改为字符串时,它会起作用

mxrecords=str(check_domain[0].exchange)

谁能解释一下为什么它接受字符串?

最佳答案

来自 https://docs.python.org/3/library/smtplib.html

你可以知道 connect() 需要一个字符串参数 host

An SMTP instance encapsulates an SMTP connection. It has methods that support a full repertoire of SMTP and ESMTP operations. If the optional host and port parameters are given, the SMTP connect() method is called with those parameters during initialization.

connect()可以点击然后重定向到一个链接,然后你可以看到:

SMTP.connect(host='localhost', port=0)
Connect to a host on a given port. The defaults are to connect to the local host at the standard SMTP port (25). If the hostname ends with a colon (':') followed by a number, that suffix will be stripped off and the number interpreted as the port number to use. This method is automatically invoked by the constructor if a host is specified during instantiation. Returns a 2-tuple of the response code and message sent by the server in its connection response.

在那里你可以知道参数 host='localhost' 默认是一个字符串。


编辑

我检查了你的代码,

print(type(mxrecords)) 

打印

<class 'dns.name.Name'>

声明 mxrecords 对象是一个 dns.name.Name 对象,而不是字符串。

如果你点击connect方法的源代码,你会发现host应该是一个字符串:

def connect(self, host='localhost', port=0, source_address=None):
"""Connect to a host on a given port.

If the hostname ends with a colon (`:') followed by a number, and
there is no port specified, that suffix will be stripped off and the
number interpreted as the port number to use.

Note: This method is automatically invoked by __init__, if a host is
specified during instantiation.

"""

if source_address:
self.source_address = source_address

if not port and (host.find(':') == host.rfind(':')):
i = host.rfind(':')
if i >= 0:
host, port = host[:i], host[i + 1:]
try:
port = int(port)
except ValueError:
raise OSError("nonnumeric port")
if not port:
port = self.default_port
if self.debuglevel > 0:
self._print_debug('connect:', (host, port))
self.sock = self._get_socket(host, port, self.timeout)
self.file = None
(code, msg) = self.getreply()
if self.debuglevel > 0:
self._print_debug('connect:', repr(msg))
return (code, msg)

并且在代码中您可以找到 host.find(':') == host.rfind(':'),这与您的错误相匹配。


查看dns.name.Name源码,您会发现Name类有一个to_text方法:

def to_text(self, omit_final_dot=False):
"""Convert name to text format.
@param omit_final_dot: If True, don't emit the final dot (denoting the
root label) for absolute names. The default is False.
@rtype: string
"""

if len(self.labels) == 0:
return maybe_decode(b'@')
if len(self.labels) == 1 and self.labels[0] == b'':
return maybe_decode(b'.')
if omit_final_dot and self.is_absolute():
l = self.labels[:-1]
else:
l = self.labels
s = b'.'.join(map(_escapify, l))
return maybe_decode(s)

因此您应该使用 mxrecords.to_text() 来获取 MX 服务器名称。

关于python - 为什么 mxrecord 应该改为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48071359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com