#!/usr/bin/env python3"""Unpack a MIME message into a directory of files."""import osimport emailimport mimetypesfrom email.policy import defaultfrom argparse import ArgumentParserdef main():parser = ArgumentParser(description="""\Unpack a MIME message into a directory of files.""")parser.add_argument('-d', '--directory', required=True,help="""Unpack the MIME message into the nameddirectory, which will be created if it doesn't alreadyexist.""")parser.add_argument('msgfile')args = parser.parse_args()with open(args.msgfile, 'rb') as fp:msg = email.message_from_binary_file(fp, policy=default)try:os.mkdir(args.directory)except FileExistsError:passcounter = 1for part in msg.walk():# multipart/* are just containersif part.get_content_maintype() == 'multipart':continue# Applications should really sanitize the given filename so that an# email message can't be used to overwrite important filesfilename = part.get_filename()if not filename:ext = mimetypes.guess_extension(part.get_content_type())if not ext:# Use a generic bag-of-bits extensionext = '.bin'filename = 'part-%03d%s' % (counter, ext)counter += 1with open(os.path.join(args.directory, filename), 'wb') as fp:fp.write(part.get_payload(decode=True))if __name__ == '__main__':main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。