GWo*_*ing 2 email gmail with-statement python-3.x raspberry-pi
我想知道为什么这个代码
test = smtplib.SMTP('smtp.gmail.com', 587)
test.ehlo()
test.starttls()
test.ehlo()
test.login('address','passw')
test.sendmail(sender, recipients, composed)
test.close()
Run Code Online (Sandbox Code Playgroud)
工作,但写这样的
with smtplib.SMTP('smtp.gmail.com', 587) as s:
s.ehlo()
s.starttls()
s.ehlo()
s.login('address','passw')
s.sendmail(sender, recipients, composed)
s.close()
Run Code Online (Sandbox Code Playgroud)
它失败了
Unable to send the email. Error: <class 'AttributeError'>
Traceback (most recent call last):
File "py_script.py", line 100, in <module>
with smtplib.SMTP('smtp.gmail.com', 587) as s:
AttributeError: __exit__
Run Code Online (Sandbox Code Playgroud)
为什么会这样?(覆盆子pi上的python3)Thx
您没有使用Python 3.3或更高版本.在您的Python版本中,smtplib.SMTP()不是上下文管理器,不能在with语句中使用.
由于没有__exit__方法,需要上下文管理器,因此直接导致回溯.
版本3.3中已更改:
with添加了对语句的支持.
您可以将对象包装在上下文管理器中@contextlib.contextmanager:
from contextlib import contextmanager
from smtplib import SMTPResponseException, SMTPServerDisconnected
@contextmanager
def quitting_smtp_cm(smtp):
try:
yield smtp
finally:
try:
code, message = smtp.docmd("QUIT")
if code != 221:
raise SMTPResponseException(code, message)
except SMTPServerDisconnected:
pass
finally:
smtp.close()
Run Code Online (Sandbox Code Playgroud)
这使用与Python 3.3中添加的相同的退出行为.像这样使用它:
with quitting_smtp_cm(smtplib.SMTP('smtp.gmail.com', 587)) as s:
s.ehlo()
s.starttls()
s.ehlo()
s.login('address','passw')
s.sendmail(sender, recipients, composed)
Run Code Online (Sandbox Code Playgroud)
请注意,它会为您关闭连接.