[Tutor] Decoding MIME Attachments

Wayne Werner waynejwerner at gmail.com
Fri Apr 2 03:10:35 CEST 2010

On Thu, Apr 1, 2010 at 5:47 PM, <jneiss at neisskids.org> wrote:

>  Hi, all;
>  I am a longtime linux sysadmin that is fairly new to Python. I've got a
> project for which Python seems to be perfect (or, at least, I've found a
> way to integrate my learning :-)

Welcome to Python! I highly recommend this book by Noah Gift:

It seems like it's right up your alley!

>  I receive log files via email from an outside vendor. We use Splunk to
> generate reports based on these and other logs. At the moment, pulling
> the emailed logs into Splunk is done by hand--open the email, save the
> attachment, read the file in. I'd like to automate it, and procmail + a
> script is the easiest way. I've found perl scripts that supposedly pull
> MIME-encoded attachments, but not being a perl guy, I'm trying to use
> Python instead.
>  I have the following script--cobbled together from Internet searches, the
> Python cookbook, and hand-written lines:
> #! /usr/bin/env python
> import email.Parser
> import os, sys
> def main(argv = sys.argv):
>      if not sys.stdin.isatty():
>              for m in sys.stdin.readlines():
>                p = email.Parser.Parser()
>                msg = p.parse(m)

Here's basically where the problem is - m is a string. From the docs (I use
ipython - in the regular python prompt you'd say >>> help(p.parse):

In [3]: p.parse?
Type:           instancemethod
Base Class:     <type 'instancemethod'>
String Form:    <bound method Parser.parse of <email.parser.Parser instance
at 0x92ffa8c>>
Namespace:      Interactive
File:           /usr/lib/python2.6/email/parser.py
Definition:     p.parse(self, fp, headersonly=False)
    Create a message structure from the data in a file.

    Reads all the data from the file and returns the root of the message
    structure.  Optional headersonly is a flag specifying whether to stop
    parsing after reading the headers or not.  The default is False,
    meaning it parses the entire contents of the file.

So parse is looking for a file - the fp parameter.

> <snip>
>  Any ideas? What am I doing wrong?
>  I'm using Python 2.4.3 on Red Hat ES 5.3, if it matters.
See if you can figure it out from there - if you get further, and get stuck
again, let us know!


p.s. bonus points for posting the Traceback. A bit of explanation (read from
bottom to top)

 File "./mail_extract.py", line 32, in ?              <----- The main block
where it was called
 File "./mail_extract.py", line 12, in main         <-------- This is where
the next line was called
   msg = p.parse(mailfile)
 File "/usr/lib64/python2.4/email/Parser.py", line 65, in parse    <---
Tells you where the original error was (usually)
   data = fp.read(8192)
AttributeError: 'str' object has no attribute 'read'       <--------- Really
important, tells you what went wrong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100401/e547c2d4/attachment.html>

More information about the Tutor mailing list