我正在使用由服务器(名称,IP)元组组成的列表。我正在使用ping测试每个服务器的连接性。如果ping失败,则将其添加到名为issues
的列表中。我试图通过电子邮件将这个失败列表发送给我自己,因为有任何失败。我不确定自己出了什么问题,但我收到了以下错误:
Traceback (most recent call last):
File "C:/Python27/Scripts/serverconnection.py", line 26, in <module>
msg['SUBJECT'] = "Server Disconnect Notice"
TypeError: 'str' object does not support item assignment
产生错误的代码在
之下if len(issues) > 0 :
body = '\n'.join('%s, %s' % server for server in issues)
msg = body
msg['SUBJECT'] = "Server Disconnect Notice"
msg['FROM'] = "Alli Deacon"
msg['TO'] = 'allid@atlanticpkg.com'
msg.attach(text)
答案 0 :(得分:0)
因为 msg 是 str ,所以你不能像在dict中那样msg['SUBJECT']
尝试以下方法:
msg = dict()
msg['SUBJECT'] = "Server Disconnect Notice"
msg['FROM'] = "Alli Deacon"
msg['TO'] = 'allid@atlanticpkg.com'
msg.send() # i don't know what module for e-mails you use
答案 1 :(得分:0)
我需要以下导入:
from email.MIMEText import MIMEText
然后使用以下内容更新我的代码:
msg = MIMEText(body)
我更新了这些添加后,代码运行正常。