Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most Efficient Way to "Slurp" All of STDIN Into a String

I'm writing an email parser for python 2.7 that will be invoked via sendmail using an alias, parsed using the email module and then processed and stored into an oracle database:

From /etc/aliases:

myalias: | /my/python/script.py

I'm having trouble "slurping" all of stdin into a string object that I can use with the email module:

import email

# Slurp stdin and store into message
message =

msg = email.message_from_string(message)

# Do something with it
print msg['Subject']

What would be the most efficient way to do this? I've tried stdin.readlines() but it ends up as a list.

Thx for any help. (Sorry if this is noobish... I'm a perl convert and been forced to standardize my tools using python and this is only my second script. Well not really "forced", I've been wanting to do this for some time but not under the gun of a deadline like now)

like image 740
BenH Avatar asked Jul 07 '14 20:07

BenH


1 Answers

sys.stdin.readlines() returns a list of lines. If what you want is one long (multi-line) string, use sys.stdin.read().

like image 186
shx2 Avatar answered Oct 17 '22 02:10

shx2