abort: command: FETCH => [OVERQUOTA] Account exceeded command or bandwidth limits

刘超 10天前 ⋅ 56 阅读   编辑

一、描述

  python使用2.58版本的imaplib拉取google邮件,报如下错误

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "./ssp_report/Google.py", line 86, in Google
    readDF = getDataFromEmail(args)
  File "./ssp_report/Google.py", line 51, in getDataFromEmail
    typ,data=M.fetch(num, '(RFC822)')
  File "/usr/lib/python2.7/imaplib.py", line 455, in fetch
    typ, dat = self._simple_command(name, message_set, message_parts)
  File "/usr/lib/python2.7/imaplib.py", line 1087, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.7/imaplib.py", line 911, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
abort: command: FETCH => [OVERQUOTA] Account exceeded command or bandwidth limits.

二、分析

  1、原来示例代码如下

import imaplib, string, email

M = imaplib.IMAP4_SSL("imap.gmail.com","993")
M.login("***","****")

# choose dir
M.select("googleABReport/****")

days=['20210110','20210111']

# search attachment
typ,datalist=M.search(None,'FROM', '***@google.com')
for num in datalist[0].split():
    typ,data=M.fetch(num, '(RFC822)')
    mail = email.message_from_string(data[0][1])
    # print "email recive time %s"%(mail.get("Date"))
    # receiveDay=datetime.datetime.strptime(mail.get("Date"),"%a, %d %b %Y %H:%M:%S %Z").strftime('%Y%m%d')
    receiveDay=parse(mail.get("Date")).strftime('%Y%m%d')

    if receiveDay in days:
        print receiveDay

  2、修改示例如下

for day in days:
    beginDay=datetime.datetime.strptime(day,"%Y%m%d").strftime('%d-%b-%Y')
    endDay=(datetime.datetime.strptime(day,"%Y%m%d")-datetime.timedelta(-1)).strftime('%d-%b-%Y')
    # print beginDay
    # '11-Jan-2021'
    # print endDay
    # '12-Jan-2021'
    typ,datalist=M.search(None, "(SINCE \"%s\" BEFORE \"%s\")"%(beginDay,endDay))

    if datalist and datalist[0]:
        typ,data=M.fetch(72, '(RFC822)')
        mail = email.message_from_string(data[0][1])
        receiveDay=parse(mail.get("Date")).strftime('%Y%m%d')
        print receiveDay
        # 20210111

# >>> M.search(None, '(SINCE "01-Jan-2021")')
# ('OK', ['62 63 64 65 66 67 68 69 70 71 72 73'])

# >>> M.search(None, '(SINCE "01-Jan-2021" BEFORE "02-Jan-2021")')
# ('OK', ['62'])

# >>> day='20210111'
# >>> beginDay=datetime.datetime.strptime(day,"%Y%m%d").strftime('%d-%b-%Y')
# >>> endDay=(datetime.datetime.strptime(day,"%Y%m%d")-datetime.timedelta(-1)).strftime('%d-%b-%Y')
# >>> beginDay
# '11-Jan-2021'
# >>> endDay
# '12-Jan-2021'
# >>> M.search(None, "(SINCE \"%s\" BEFORE \"%s\")"%(beginDay,endDay))
# ('OK', ['72'])
# >>> typ,data=M.fetch(72, '(RFC822)')
# >>> mail = email.message_from_string(data[0][1])
# >>> receiveDay=parse(mail.get("Date")).strftime('%Y%m%d')
# >>> print receiveDay
# 20210111



>>> M.search(None, '(SINCE "01-01-2021" BEFORE "02-01-2021")')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/imaplib.py", line 639, in search
    typ, dat = self._simple_command(name, *criteria)
  File "/usr/lib/python2.7/imaplib.py", line 1087, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.7/imaplib.py", line 917, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SEARCH command error: BAD ['Could not parse command']

三、参考文章
  1、https://support.google.com/a/answer/1071518?hl=zh-Hant
  2、https://stackoverflow.com/questions/5621341/search-before-after-with-pythons-imaplib  
  3、https://stackoverflow.com/questions/28597779/how-to-filter-gmail-imap-messages-for-date-in-python


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: