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 :-)
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) mailfile.close() partcounter = 1 for part in msg.walk(): if part.get_content_maintype() == "multipart": continue name = part.get_param("name") if name == None: name = "part-%i" % partcounter partcounter += 1 print "partcounter = %s" % partcounter if name != "part-1": outfile = open(name, "wb") outfile.write(part.get_payload(decode=1)) outfile.close() if __name__=="__main__": try: main(sys.argv) except KeyboardInterrupt: pass The gist is: read whatever's piped to the script (from procmail or on the command line), walk through the data looking for the second MIME header, and write that part (the file attachment) to disk. What actually happens is this: [u...@server scripts]# cat /tmp/mail.txt | ./mail_extract.py Traceback (most recent call last): File "./mail_extract.py", line 32, in ? main(sys.argv) File "./mail_extract.py", line 12, in main msg = p.parse(mailfile) File "/usr/lib64/python2.4/email/Parser.py", line 65, in parse data = fp.read(8192) AttributeError: 'str' object has no attribute 'read' [u...@server scripts]# f I change the three lines starting with "def main" to: def main(): if len(sys.argv) != 2: print "Usage: %s filename" % os.path.basename(sys.argv[0]) sys.exit(1) and remove the last five lines (if...pass), changing the command line to "./mail_extract.py /tmp/mail.txt", it works like a champ. As is, it errors whether through procmail or directly from the command line. Any ideas? What am I doing wrong? I'm using Python 2.4.3 on Red Hat ES 5.3, if it matters. Thanks; Jason -- ja...@neisskids.org gentoo linux, as if you cared nac mac feegle! _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor