我想为用户在我的脚本中输入错误密码编写错误处理。我在 except 语句之后不断更改代码,但我无法找出错误的正确代码是什么。我错过了什么吗?
import imaplib
import email
mail = imaplib.IMAP4_SSL('imap.gmail.com')
username = raw_input('USERNAME (email):')
password = raw_input('PASSWORD: ')
try:
mail.login(username, password)
print "Logged in as %r !" % username
except:
imaplib.error
print "Log in failed."
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
fromWho = raw_input('FROM:')
result, data = mail.uid('search', None, '(FROM fromWho)')
latest_email_uid = data[0].split()[-1]
result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data[0][1]
print raw_email