[英]TypeError: 'NoneType' object is not iterable :smtplib
def sendmail(self, from_addr, to_addrs, msg='hello', mail_options=[],rcpt_options=[]):
self.ehlo_or_helo_if_needed()
esmtp_opts = []
if self.does_esmtp:
# Hmmm? what's this? -ddm
# self.esmtp_features['7bit']=""
if self.has_extn('size'):
esmtp_opts.append("size=%d" % len(msg))
for option in mail_options:
esmtp_opts.append(option)
(code, resp) = self.mail(from_addr, esmtp_opts)
if code != 250:
self.rset()
#raise SMTPSenderRefused(code, resp, from_addr)
senderrs = {}
if isinstance(to_addrs, basestring):
to_addrs = [to_addrs]
for each in to_addrs:
(code, resp) = self.rcpt(each, rcpt_options)
if (code != 250) and (code != 251):
senderrs[each] = (code, resp)
if len(senderrs) == len(to_addrs):
# the server refused all our recipients
self.rset()
#raise SMTPRecipientsRefused(senderrs)
(code, resp) = self.data(msg)
if code != 250:
self.rset()
#raise SMTPDataError(code, resp)
#if we got here then somebody got our mail
return senderrs
我正在尝试构建自己的smtplib.getTypeError:我尝试时'NoneType'对象不可迭代
(代码,分别)= self.data(msg)
self.data()的定义为-我正在使用self.data()将消息发送到服务器。
def data(self, msg):
self.putcmd("data")
(code, repl) = self.getreply()
if self.debuglevel > 0:
print>>stderr, "data:", (code, repl)
if code != 354:
#raise SMTPDataError(code, repl)
print "Err"
else:
q = quotedata(msg)
if q[-2:] != CRLF:
q = q + CRLF
q = q + "." + CRLF
self.send(q)
(code, msg) = self.getreply()
if self.debuglevel > 0:
print>>stderr, "data:", (code, msg)
return (code, msg)
从代码中我们无法确定您已经发布了self.data()
在做什么,但是从您的错误中可以清楚地看到它返回None
。 如果您尝试打开无包装,则会得到以下信息:
>>> (code, resp) = None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
为self.data()
发布代码可能会有所帮助。
发布更多代码后进行更新:
代码(code, repl) = self.getreply()
也可以解(code, repl) = self.getreply()
None
[注意: self.data()
有两个实例self.data()
。 是在self.data()
还是sendmail()
引发异常?
也
if code != 354:
#raise SMTPDataError(code, repl)
print "Err"
else:
...
如果获得代码354,则已注释掉raise
,该函数现在返回None。 那可能也是问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.