def _get(self, remote_filename, local_path):
allowedTimeout = globals.timeout
if (allowedTimeout == 0):
# Allow a total timeout of 1 day
allowedTimeout = 2880
while allowedTimeout > 0:
try:
self.conn.select(globals.imap_mailbox)
(result, list) = self.conn.search(None, 'Subject', remote_filename)
if result != "OK":
raise Exception(list[0])
# check if there is any result
if list[0] == '':
raise Exception("no mail with subject %s")
(result, list) = self.conn.fetch(list[0], "(RFC822)")
if result != "OK":
raise Exception(list[0])
rawbody = list[0][1]
p = email.Parser.Parser()
m = p.parsestr(rawbody)
mp = m.get_payload(0)
body = mp.get_payload(decode=True)
break
except (imaplib.IMAP4.abort, socket.error, socket.sslerror):
allowedTimeout -= 1
log.Info("Error loading '%s', retrying in 30s " % remote_filename)
time.sleep(30)
while allowedTimeout > 0:
try:
self.resetConnection()
break
except (imaplib.IMAP4.abort, socket.error, socket.sslerror):
allowedTimeout -= 1
log.Info("Error reconnecting, retrying in 30s ")
time.sleep(30)
tfile = local_path.open("wb")
tfile.write(body)
local_path.setdata()
log.Info("IMAP mail with '%s' subject fetched" % remote_filename)
评论列表
文章目录