gpt4 book ai didi

python - 类型错误 : string payload expected:

转载 作者:行者123 更新时间:2023-12-01 06:02:16 27 4
gpt4 key购买 nike

我正在使用Python 3.2。我使用以下代码编写了构建日志文件:

rsltFile = open('buildLog.txt', 'wb')
p = subprocess.Popen('call ant compile', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
rsltFile.write(line)
retval = p.wait()

然后我使用以下命令发送附有上述文件的电子邮件:

def send_mail(send_from, send_to, subject, text, files=[], server="XXX.XXX.com"):
assert type(send_to)==list
assert type(files)==list

msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject

msg.attach( MIMEText(text) )

for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)

smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()

但我收到以下错误:

  File "C:\workspace\VCT2400_Service\ServiceApplication\autobuild\myMail.py", line 29, in send_mail
smtp.sendmail(send_from, send_to, msg.as_string())
File "C:\Python32\lib\email\message.py", line 168, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Python32\lib\email\generator.py", line 91, in flatten
self._write(msg)
File "C:\Python32\lib\email\generator.py", line 137, in _write
self._dispatch(msg)
File "C:\Python32\lib\email\generator.py", line 163, in _dispatch
meth(msg)
File "C:\Python32\lib\email\generator.py", line 224, in _handle_multipart
g.flatten(part, unixfrom=False, linesep=self._NL)
File "C:\Python32\lib\email\generator.py", line 91, in flatten
self._write(msg)
File "C:\Python32\lib\email\generator.py", line 137, in _write
self._dispatch(msg)
File "C:\Python32\lib\email\generator.py", line 163, in _dispatch
meth(msg)
File "C:\Python32\lib\email\generator.py", line 192, in _handle_text
raise TypeError('string payload expected: %s' % type(payload))
TypeError: string payload expected: <class 'bytes'>

最佳答案

Python 3 中的 file.read(以“rb”模式打开时)返回字节,您需要以“rt”模式打开文件或使用将字节解码为字符串。

我强烈建议观看http://pyvideo.org/video/948/pragmatic-unicode-or-how-do-i-stop-the-pain

关于python - 类型错误 : string payload expected: <class 'bytes' >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9772431/

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