多线程IMAP备份[python]

时间:2017-11-16 15:12:27

标签: python multithreading imap

我有以下python脚本,可以将IMAP文件夹保存到本地.eml文件。我想备份我的旧电子邮件,但它有相当大的收件箱。无论如何,以下代码可以用多线程修改?或者可能是几个循环,以便它在大型IMAP文件夹上运行得更快?

谢谢你!

import sys
import imaplib
import getpass

IMAP_SERVER = ''
EMAIL_ACCOUNT = ""
EMAIL_FOLDER = "Inbox"
OUTPUT_DIRECTORY = './'

PASSWORD = getpass.getpass()


def process_mailbox(M):
    """
    Dump all emails in the folder to files in output directory.
    """

    rv, data = M.search(None, "ALL")
    if rv != 'OK':
        print "No messages found!"
        return

    for num in data[0].split():
        rv, data = M.fetch(num, '(RFC822)')
        if rv != 'OK':
            print "ERROR getting message", num
            return
        print "Writing message ", num

        f = open('%s/%s.eml' %(OUTPUT_DIRECTORY, num), 'wb')
        f.write(data[0][1])
        f.close()

def main():
    M = imaplib.IMAP4_SSL(IMAP_SERVER)
    M.login(EMAIL_ACCOUNT, PASSWORD)
    rv, data = M.select(EMAIL_FOLDER)
    if rv == 'OK':
        print "Processing mailbox: ", EMAIL_FOLDER
        process_mailbox(M)
        M.close()
    else:
        print "ERROR: Unable to open mailbox ", rv
    M.logout()

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

您是否尝试过一次抓取多条消息? Fetch能够获取多条消息:m.fetch("1:*", (RFC822)")将获取邮箱中的所有邮件,但这是耗尽内存的好方法。

如果你有很多记忆,也许可以尝试一百个批量提取。这可能会加速大约20至50倍,因为瓶颈通常是网络往返。