Python:Imaplib错误 [英] Python: Imaplib error

查看:449
本文介绍了Python:Imaplib错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 导入序列
导入imaplib
导入睡眠

IMAP_SERVER ='imap.gmail.com'
IMAP_PORT = 993
ser = serial.Serial('/ dev / ttyACM0',9600)

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.搜索(INBOX,UNDELETED)[1] [0] .split():
msg = M.fetch('1','(BODY.PEEK [TEXT])')
尝试:
String = msg [1] [0] [1] [139:148]
TypeError除外:
继续

打印字符串
if String ==这只是一个测试...:
ser.write('0')
sleep(1)

我是一名python编程的新手,上面的python代码是我用来编写程序的代码。当我在终端上运行这个程序时,我收到了我对我的帐户进行了身份验证的响应,然后显示了字符139& 161,这是电子邮件示例中的以下内容:

 这只是一个测试... 

这是打印出来的终端。几次后程序检查我的电子邮件出现这个错误:

  Traceback(最近一次调用的最后一次):
在< module>文件中,第11行的文件/ home / wilson / Desktop / Best_Gmail_yet _Dont_touch.py​​
rc,resp = M.login('user@gmail.com','password')
文件/usr/lib/python2.6/imaplib.py,行500,在登录
提高self.error(dat [-1])$ ​​b $ b imaplib.error:[ALERT]需要Web登录:http://mail.google.com/support/bin/answer.py?answer=78754 (失败)

有没有人有任何想法可以帮忙,还有其他方法可以写入串行,提前致谢!

解决方案

从错误消息url( http://mail.google.com/support/bin/answer.py?answer=78754 ):


请确保您的邮件客户端没有设置
,以便经常检查新邮件。如果
您的邮件客户端每10个b $ b分钟检查一次新
邮件超过一次,您的客户可能会重复
请求您的用户名和密码。


我猜你会频繁地连接到服务器,并且gmail会感到怀疑。

你似乎也打开了多个imap连接而没有关闭任何连接。我不知道你想要做什么,但我猜想有一种更简洁的方式,可能只涉及一个你不断维护和轮询的连接。


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!

解决方案

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.

这篇关于Python:Imaplib错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆