问题
import serial
import imaplib
from time import sleep
IMAP_SERVER='imap.gmail.com'
IMAP_PORT=993
ser= serial.Serial ('/dev/ttyACM0',9600)
while True:
M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
rc, resp = M.login('user@gmail.com', 'Password')
print rc, resp
M.select()
for msg_num in M.search("INBOX", "UNDELETED")[1][0].split():
msg = M.fetch('1', '(BODY.PEEK[TEXT])')
try:
String = msg[1][0][1][139:148]
except TypeError:
continue
print String
if String == "This is just a test...":
ser.write('0')
sleep(1)
I'm a new beginner in python programming and the above python code is one that I'm using for a program I want to do. When I run this in a terminal I get the response that I have authenticated my account and then it displays the message between characters 139 & 161, which is the following in the example email:
This is just a test...
This is printed out in the terminal. After a few times the program checks my email this error comes out:
Traceback (most recent call last):
File "/home/wilson/Desktop/Best_Gmail_yet _Dont_touch.py", line 11, in <module>
rc, resp = M.login('user@gmail.com', 'password')
File "/usr/lib/python2.6/imaplib.py", line 500, in login
raise self.error(dat[-1])
imaplib.error: [ALERT] Web login required: http://mail.google.com/support /bin/answer.py?answer=78754 (Failure)
Does anyone have any ideas to help out and is there any other way to write to serial, Thanks in advance!
回答1:
From the error message url (http://mail.google.com/support/bin/answer.py?answer=78754):
Make sure your mail client isn't set to check for new mail too often. If your mail client checks for new messages more than once every 10 minutes, your client might repeatedly request your username and password.
I'd guess you're connecting to the server too frequently, and gmail gets suspicious.
You also appear to be opening multiple imap connections without closing any of them. I don't know exactly what you're trying to do but I'd guess there's a more parsimonious way, probably involving just one connection that you maintain and poll from time to time.
来源:https://stackoverflow.com/questions/5479240/python-imaplib-error