我忘记了我的gmail密码,所以我想创建一个包含所有密码的列表..希望其中一个密码正确,所以我在下面编写了代码
import smtplib
server=smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
passwordList=["cows","dogs",monkeys","water"]
try:
for password in passwordList:
server.login("MyEmail@gmail.com",password)
except smtplib.SMTPAuthenticationError:
server.login("MyEmail@gmail.com",password)
#i need a code that will now uses the second string in the list password..what do you recommend i do
#since after the execution of the top part password will be put as cows and if it is not the correct one..the error smtplib.SMTPAuthenticationError will be given and the program will stop so i added.try and except
server.sendmail("MyEmail@gmail.com",Someone@gmail.com","password found")
答案 0 :(得分:0)
只要把它放在那里,如果你要写出密码列表,为什么不尝试每一个...
反正:
你非常正确,只需将你的try
移到for循环中:
found = False
for password in passwordList:
try:
server.login("MyEmail@gmail.com",password)
#anything past this line will ONLY be executed if the above line doesnt run an error (ie, the password was correct)
found = True #change the boolean to True since the above line ran properly
break #we already found the password so we can break out of the for loop
except smtplib.SMTPAuthenticationError:
pass #since its the wrong password, just go on to the next password
if found: #will only execute if found was changed to true
server.sendmail("MyEmail@gmail.com",Someone@gmail.com","password found")
found
bool以防你通过列表并且没有任何内容有效。