piping an email message
Alessio Pace
puccio_13 at yahoo.it
Fri May 16 15:33:45 EDT 2003
Gerhard Häring wrote:
> Alessio Pace wrote:
>> Hi, I am writing a filter for the emails, I need it to pass through stdin
>> and then go via stdout.
>>
>> Would this piece of code be okay to let just pass the message through the
>> pipe correctly?
>>
>> # file 'pipe.py'
>> import email
>> import sys
>>
>> def main():
>> stdin_message = sys.stdin.read()
>> msg = email.message_from_string(stdin_message)
>> sys.stdout.write(str(msg))
>>
>> if __name__ == '__main__':
>> main()
>
> The problem with this code is that it needs to read the entire message
> into memory. For 90 % of the messages, this should be no problem, but
> for the few multi-megabyte messages that you'll probably process, it
> will have negative impacts on the system performance.
>
> I'd rather do something like:
>
> #v+
> CHUNK_SIZE = 1024
>
> while True:
> chunk = sys.stdin.read(CHUNK_SIZE)
> if not chunk: break
> sys.stdout.write(chunk)
> #v-
>
> Now that you know how to do it properly, you can use the relevant
> library function in the first place :-)
>
> #v+
> import shutil
> shutil.copyfileobj(sys.stdin, sys.stdout)
> #v-
>
> As you'll read strings, anyway, the explicit conversion to string using
> str() in your above code is superfluous, btw.
Mine was an example. In the real application I would modify the message read
from stdin and then write it to stdout.
Thanks for the rest
>
> -- Gerhard
--
bye
Alessio Pace
More information about the Python-list
mailing list