FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Doc/includes/email-unpack.py at 3.5 · python-emscripten/cpython · GitHub
python-emscripten
cpython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Doc
/
includes
/
email-unpack.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (43 loc) · 1.51 KB
Breadcrumbs
cpython
/
Doc
/
includes
/
email-unpack.py
Copy path
File metadata and controls
53 lines (43 loc) · 1.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
"""Unpack a MIME message into a directory of files."""
import
os
import
sys
import
email
import
errno
import
mimetypes
from
argparse
import
ArgumentParser
def
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 named
directory, which will be created if it doesn't already
exist."""
)
parser
.
add_argument
(
'msgfile'
)
args
=
parser
.
parse_args
()
with
open
(
args
.
msgfile
)
as
fp
:
msg
=
email
.
message_from_file
(
fp
)
try
:
os
.
mkdir
(
args
.
directory
)
except
FileExistsError
:
pass
counter
=
1
for
part
in
msg
.
walk
():
# multipart/* are just containers
if
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 files
filename
=
part
.
get_filename
()
if
not
filename
:
ext
=
mimetypes
.
guess_extension
(
part
.
get_content_type
())
if
not
ext
:
# Use a generic bag-of-bits extension
ext
=
'.bin'
filename
=
'part-%03d%s'
%
(
counter
,
ext
)
counter
+=
1
with
open
(
os
.
path
.
join
(
args
.
directory
,
filename
),
'wb'
)
as
fp
:
fp
.
write
(
part
.
get_payload
(
decode
=
True
))
if
__name__
==
'__main__'
:
main
()
Back
|
FazBrowse Home
|
New Git URL