[英]Python: Gmail Unread Mails Crashes
import imaplib, re
import os
import time
import socket
imap_host = 'imap.gmail.com'
mail = imaplib.IMAP4_SSL(imap_host)
mail.login("xxx@example.com", "sddd")
while True:
try:
print 'Connecting to Inbox..'
mail.select("inbox") # connect to inbox.
result, data = mail.uid('search', None, 'UNSEEN')
uid_list = data[0].split()
print len(uid_list), 'Unseen emails.'
if len(uid_list) > 20:
os.system('heroku restart --app xx-xx-203')
time.sleep(30)
except:
print 'Error'
imap_host = 'imap.gmail.com'
mail = imaplib.IMAP4_SSL(imap_host)
mail.login("xxx@example.com", "xxx")
pass
Works perfectly but sometimes it crashes with: 工作完美,但有时崩溃:
Restarting processes... done
Connecting to Inbox..
Error
Traceback (most recent call last):
File "gmail_new9.py", line 24, in <module>
mail.login("xxx@ccc.com", "ddddd")
File "/usr/lib/python2.6/imaplib.py", line 498, in login
typ, dat = self._simple_command('LOGIN', user, self._quote(password))
File "/usr/lib/python2.6/imaplib.py", line 1060, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/usr/lib/python2.6/imaplib.py", line 893, in _command_complete
self._check_bye()
File "/usr/lib/python2.6/imaplib.py", line 808, in _check_bye
raise self.abort(bye[-1])
imaplib.abort: [UNAVAILABLE] Temporary System Error
How can I fix this? 我怎样才能解决这个问题?
The reason your script crashes is that call to mail.login() inside "except" block throws an exception that is never caught. 您的脚本崩溃的原因是调用“except”块中的mail.login()会抛出一个永远不会被捕获的异常。
Documentation to imaplib states that when you get imaplib.abort exception, you should just retry you command. imaplib的文档说明当你得到imaplib.abort异常时,你应该重试你的命令。
http://docs.python.org/library/imaplib http://docs.python.org/library/imaplib
exception IMAP4.abort IMAP4 server errors cause this exception to be raised.
异常IMAP4.abort IMAP4服务器错误导致引发此异常。 This is a sub-class of IMAP4.error.
这是IMAP4.error的子类。 Note that closing the instance and instantiating a new one will usually allow recovery from this exception.
请注意,关闭实例并实例化新实例通常允许从此异常中恢复。
Also 也
>>> help('imaplib')
says the same: 说同样的话:
"abort" exceptions imply the connection should be reset, and the command re-tried.
“abort”异常意味着应重置连接,并重新尝试该命令。
Here is how you can fix it: 以下是如何解决它:
import imaplib, re
import os
import time
import socket
def connect(retries=5, delay=3):
while True:
try:
imap_host = 'imap.gmail.com'
mail = imaplib.IMAP4_SSL(imap_host)
mail.login("xxx@example.com", "sddd")
return mail
except imaplib.IMAP4_SSL.abort:
if retries > 0:
retries -= 1
time.sleep(delay)
else:
raise
mail = connect()
while True:
try:
print 'Connecting to Inbox..'
mail.select("inbox") # connect to inbox.
result, data = mail.uid('search', None, 'UNSEEN')
uid_list = data[0].split()
print len(uid_list), 'Unseen emails.'
if len(uid_list) > 20:
os.system('heroku restart --app xx-xx-203')
time.sleep(30)
except:
print 'Error'
mail = connect()
Where is the port number for imap? imap的端口号在哪里? Isnt that required?
不是必须的吗? I have been using the below code & it works.
我一直在使用以下代码,它的工作原理。 Check if it works for you too -
检查它是否也适合你 -
import imaplib
gmail = imaplib.IMAP4_SSL('imap.gmail.com',993)
gmail.login('username','password')
gmail.select("inbox")
result, data = gmail.uid('search', None, 'UNSEEN')
You could also try Gmail.py . 您也可以尝试使用Gmail.py . I tried using this simple script which abstracts simple imap calls.
我尝试使用这个简单的脚本来抽象简单的imap调用。
from gmail import *
gmail = GmailClient()
gmail.login('username','password')
unreadMail = gmail.get_inbox_conversations(is_unread=True)
print unreadMail
Be aware!! 意识到!! Gmail IMAP has known issues with clients requesting to authenticate "too often."
Gmail IMAP 已知客户端要求“过于频繁”进行身份验证的问题 。 Among other things, this may flag your account to require passing a CAPTCHA to continue syncing.
除此之外,这可能会标记您的帐户要求通过CAPTCHA以继续同步。 Visit here to attempt the unlock and then try again.
访问此处尝试解锁,然后重试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.