<div class="gmail_quote">On Thu, Apr 1, 2010 at 5:47 PM,  <span dir="ltr">&lt;<a href="mailto:jneiss@neisskids.org">jneiss@neisskids.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

 Hi, all;<br>
<br>
 I am a longtime linux sysadmin that is fairly new to Python. I&#39;ve got a<br>
project for which Python seems to be perfect (or, at least, I&#39;ve found a<br>
way to integrate my learning :-)</blockquote><div><br></div><div>Welcome to Python! I highly recommend this book by Noah Gift:</div><div> <a href="http://www.amazon.com/Python-Unix-Linux-System-Administration/dp/0596515820">http://www.amazon.com/Python-Unix-Linux-System-Administration/dp/0596515820</a></div>

<div><br></div><div>It seems like it&#39;s right up your alley!</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
 I receive log files via email from an outside vendor. We use Splunk to<br>
generate reports based on these and other logs. At the moment, pulling<br>
the emailed logs into Splunk is done by hand--open the email, save the<br>
attachment, read the file in. I&#39;d like to automate it, and procmail + a<br>
script is the easiest way. I&#39;ve found perl scripts that supposedly pull<br>
MIME-encoded attachments, but not being a perl guy, I&#39;m trying to use<br>
Python instead.<br>
<br>
 I have the following script--cobbled together from Internet searches, the<br>
Python cookbook, and hand-written lines:<br>
<br>
#! /usr/bin/env python<br>
<br>
import email.Parser<br>
import os, sys<br>
def main(argv = sys.argv):<br>
      if not sys.stdin.isatty():<br>
              for m in sys.stdin.readlines():<br>
                p = email.Parser.Parser()<br>
                msg = p.parse(m)<br></blockquote><div><br></div><div>Here&#39;s basically where the problem is - m is a string. From the docs (I use ipython - in the regular python prompt you&#39;d say &gt;&gt;&gt; help(p.parse):</div>

<div><br></div><div><div>In [3]: p.parse?</div><div>Type:           instancemethod</div><div>Base Class:     &lt;type &#39;instancemethod&#39;&gt;</div><div>String Form:    &lt;bound method Parser.parse of &lt;email.parser.Parser instance at 0x92ffa8c&gt;&gt;</div>

<div>Namespace:      Interactive</div><div>File:           /usr/lib/python2.6/email/parser.py</div><div>Definition:     p.parse(self, fp, headersonly=False)</div><div>Docstring:</div><div>    Create a message structure from the data in a file.</div>

<div>    </div><div>    Reads all the data from the file and returns the root of the message</div><div>    structure.  Optional headersonly is a flag specifying whether to stop</div><div>    parsing after reading the headers or not.  The default is False,</div>

<div>    meaning it parses the entire contents of the file.</div><div><br></div><div>So parse is looking for a file - the fp parameter. </div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

&lt;snip&gt;<br>
 Any ideas? What am I doing wrong?<br>
<br>
 I&#39;m using Python 2.4.3 on Red Hat ES 5.3, if it matters.<br>
<br></blockquote><div><br></div><div>See if you can figure it out from there - if you get further, and get stuck again, let us know!</div><div><br></div><div>HTH,</div><div>Wayne</div><div><br></div><div>p.s. bonus points for posting the Traceback. A bit of explanation (read from bottom to top)</div>

<div><div><br></div><div> File &quot;./mail_extract.py&quot;, line 32, in ?              &lt;----- The main block where it was called</div><div>   main(sys.argv)</div><div> File &quot;./mail_extract.py&quot;, line 12, in main         &lt;-------- This is where the next line was called</div>

<div>   msg = p.parse(mailfile)</div><div> File &quot;/usr/lib64/python2.4/email/Parser.py&quot;, line 65, in parse    &lt;--- Tells you where the original error was (usually)</div><div>   data = fp.read(8192)</div><div>
AttributeError: &#39;str&#39; object has no attribute &#39;read&#39;       &lt;--------- Really important, tells you what went wrong</div>
</div></div>