imapbackend.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:alicloud-duplicity 作者: aliyun 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号