【发布时间】:2021-06-20 20:48:23
【问题描述】:
我正在创建一些东西,所以当它收到一封取决于主题行的电子邮件时,它现在执行一个函数,我的代码在 python 2.7 中工作并在 3.7 中运行,但给出一个错误“TypeError: can't concat int to bytes" 我没有尝试太多,因为我在网上找不到任何东西,而且我是 python 新手,请告诉我
import imaplib
import email
from time import sleep
from ssh import myRoomOff, myRoomOn
count = 0
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('EMAIL', 'PASSWORD')
mail.list()
mail.select('inbox')
#need to add some stuff in here
while True:
mail.select('inbox')
typ, data = mail.search(None, 'UNSEEN')
ids = data[0]
id_list = ids.split()
#get the most recent email id
latest_email_id = int( id_list[-1] )
#iterate through 15 messages in decending order starting with latest_email_id
#the '-1' dictates reverse looping order
for i in range( latest_email_id, latest_email_id-1, -1 ):
typ, data = mail.fetch(i, '(RFC822)' )
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
varSubject = msg['subject']
count += 1
print(str(count) + ", " + varSubject)
if varSubject == "+myRoom":
myRoomOn()
elif varSubject == "-myRoom":
myRoomOff()
else:
print("I do not understand this email!")
pass
sleep(2)
错误
Traceback (most recent call last):
File "/Users/danielcaminero/Desktop/alexaCommandThing/checkForEmail.py", line 28, in <module>
typ, data = mail.fetch(i, '(RFC822)' )
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 548, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 1230, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 988, in _command
data = data + b' ' + arg
TypeError: can't concat int to bytes
【问题讨论】:
-
能否请您发布整个堆栈跟踪
-
在 python 3 中,使用 message_from_bytes
-
@SvenEberth 什么是堆栈跟踪?
-
@SvenEberth 哦,喜欢整个错误吗?确定现在更新它
标签: python concatenation byte imaplib