Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c2c46c37
Commit
c2c46c37
authored
Apr 07, 2000
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
New example from Skip Montanaro <skip@mojam.com>.
parent
037649ea
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
23 deletions
+29
-23
Doc/lib/libmultifile.tex
Doc/lib/libmultifile.tex
+29
-23
No files found.
Doc/lib/libmultifile.tex
View file @
c2c46c37
...
...
@@ -132,29 +132,35 @@ True if the last end-of-file was for an end-of-message marker.
\subsection
{
\class
{
MultiFile
}
Example
\label
{
multifile-example
}}
% This is almost unreadable; should be re-written when someone gets time.
\sectionauthor
{
Skip Montanaro
}{
skip@mojam.org
}
\begin{verbatim}
fp = MultiFile(sys.stdin, 0)
fp.push(outer
_
boundary)
message1 = fp.readlines()
# We should now be either at real EOF or stopped on a message
# boundary. Re-enable the outer boundary.
fp.next()
# Read another message with the same delimiter
message2 = fp.readlines()
# Re-enable that delimiter again
fp.next()
# Now look for a message subpart with a different boundary
fp.push(inner
_
boundary)
sub
_
header = fp.readlines()
# If no exception has been thrown, we're looking at the start of
# the message subpart. Reset and grab the subpart
fp.next()
sub
_
body = fp.readlines()
# Got it. Now pop the inner boundary to re-enable the outer one.
fp.pop()
# Read to next outer boundary
message3 = fp.readlines()
import mimetools
import MultiFile
import StringIO
def extract
_
mime
_
part
_
matching(stream, mimetype):
"""Return the first element in a multipart MIME message on stream
matching mimetype."""
msg = mimetools.Message(stream)
msgtype = msg.gettype()
params = msg.getplist()
data = StringIO.StringIO()
if msgtype[:10] == "multipart/":
file = multifile.MultiFile(stream)
file.push(msg.getparam("boundary"))
while file.next():
submsg = mimetools.Message(file)
try:
data = StringIO.StringIO()
mimetools.decode(file, data, submsg.getencoding())
except ValueError:
continue
if submsg.gettype() == mimetype:
break
file.pop()
return data.getvalue()
\end{verbatim}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment