forked from brandon-rhodes/fopnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_structure.py
executable file
·40 lines (36 loc) · 1.54 KB
/
display_structure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter12/display_structure.py
#
# Someday this script will use part.is_attachment instead of making the
# decision itself, but http://bugs.python.org/issue21079
import argparse, email.policy, sys
def walk(part, prefix=''):
yield prefix, part
for i, subpart in enumerate(part.iter_parts()):
yield from walk(subpart, prefix + '.{}'.format(i))
def main(binary_file):
policy = email.policy.SMTP
message = email.message_from_binary_file(binary_file, policy=policy)
for prefix, part in walk(message):
line = '{} type={}'.format(prefix, part.get_content_type())
if not part.is_multipart():
content = part.get_content()
line += ' {} len={}'.format(type(content).__name__, len(content))
cd = part['Content-Disposition']
is_attachment = cd and cd.split(';')[0].lower() == 'attachment'
if is_attachment:
line += ' attachment'
filename = part.get_filename()
if filename is not None:
line += ' filename={!r}'.format(filename)
print(line)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Display MIME structure')
parser.add_argument('filename', nargs='?', help='File containing an email')
args = parser.parse_args()
if args.filename is None:
main(sys.stdin.buffer)
else:
with open(args.filename, 'rb') as f:
main(f)