GNU Info

Info Node: (python2.1-lib.info)MultiFile Example

(python2.1-lib.info)MultiFile Example


Prev: MultiFile Objects Up: multifile
Enter node , (file) or (file)node

`MultiFile' Example
-------------------

This manual section was written by Skip Montanaro <skip@mojam.com>.
     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()


automatically generated by info2www version 1.2.2.9