looking for command-pipe performance advice

Sheila King sheila at spamcop.net
Sat Apr 7 12:41:17 EDT 2001


On 6 Apr 2001 16:36:42 -0000, jason-dated-14a0ce692bc19405 at mastaler.com wrote
in comp.lang.python in article
<mailman.986575031.30964.python-list at python.org>:

:Sheila King <sheila at spamcop.net> writes:
:
:> Couldn't you just do this?
:> 
:> headers = rfc822.Message(sys.stdin, 0)
:> 
:> # header examination #
:> 
:> inject_message = os.popen('/var/qmail/bin/qmail-inject', 'w')
:> inject_message.write(headers+'\n')
:> inject_message.write(sys.stdin.read())
:> inject_message.close()
:
:This returns a TypeError for me.
:
:First:
:
:    Traceback (most recent call last):
:      File "./inject.py", line 10, in ?
:        inject_message.write(headers+'\n')
:    TypeError: __add__ nor __radd__ defined for these operands

Oops. Sorry. I wasn't trying very hard there. I have a script that filters
email/examines headers for me, and I do not use cStringIO or StringIO. I read
from standard input. Here are a few relevant lines from my code:

	origheaders=rfc822.Message(sys.stdin, 0)
	raw_body = sys.stdin.read()

	newheader = string.join(origheaders.headers,"")
	newheader += "X-Filter: some header field inserted here\n"
	line = "\n"
	body = string.join(raw_body, "")

	mssg = newheader+line+body


Then I send the message using the smtplib module.

Um, so instead of using StingIO objects, I take the input and turn it into
strings. Is this more effecient than what you were doing? I have no idea.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/


:Then after removing the '\n':
:
:    Traceback (most recent call last):
:      File "./inject.py", line 10, in ?
:        inject_message.write(headers)
:    TypeError: read-only character buffer, instance
:
:This "read-only character buffer" error was why I was using StringIO
:initially.




More information about the Python-list mailing list