<div class="gmail_quote">On Thu, Apr 1, 2010 at 5:47 PM, <span dir="ltr"><<a href="mailto:jneiss@neisskids.org">jneiss@neisskids.org</a>></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've got a<br>
project for which Python seems to be perfect (or, at least, I'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'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'd like to automate it, and procmail + a<br>
script is the easiest way. I've found perl scripts that supposedly pull<br>
MIME-encoded attachments, but not being a perl guy, I'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'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):</div>
<div><br></div><div><div>In [3]: p.parse?</div><div>Type: instancemethod</div><div>Base Class: <type 'instancemethod'></div><div>String Form: <bound method Parser.parse of <email.parser.Parser instance at 0x92ffa8c>></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;">
<snip><br>
Any ideas? What am I doing wrong?<br>
<br>
I'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 "./mail_extract.py", line 32, in ? <----- The main block where it was called</div><div> main(sys.argv)</div><div> File "./mail_extract.py", line 12, in main <-------- This is where the next line was called</div>
<div> msg = p.parse(mailfile)</div><div> File "/usr/lib64/python2.4/email/Parser.py", line 65, in parse <--- Tells you where the original error was (usually)</div><div> data = fp.read(8192)</div><div>
AttributeError: 'str' object has no attribute 'read' <--------- Really important, tells you what went wrong</div>
</div></div>