[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to pipe from python script to system process that script starts



Douglas A. Tutty wrote:
On Mon, Sep 03, 2007 at 08:05:16AM -0700, Freddy Freeloader wrote:
I'm wanting to learn python so I'm starting with projects that I want to automate at work. What I want to do in this specific instance is use a python script to call exipick to find all frozen messages in the Exim queue, then feed the message id's to something such as "exim -Mvh" so I can look at the message headers. So far I've been able to get everything working except for how to get my python script to be able to pass the message id's to the exim command as the needed single parameter. I'm assuming that a pipe is the logical way to do this, but just haven't found any kind of example for what I am wanting to do. My Python reference book is just a little too cryptic for me yet and "Learning Python" barely touches on piping. All the examples there are on how to pipe from stdin with sys.stdin and that won't work for this task.


Since exim -Mvh, as you say, only takes a single message id, you'll be
starting a new process for each message.  What you didn't say was where
you want the output to go.  If you just want to run the command and use
exim's way of displaying the info as if you had typed the command from
the shell, then you would use os.system().  If you want to get its
output back into python you would use one of the os.popen() functions,
depending on what pipes you want.  Probably just os.popen() with its
mode defaulting to 'r'.

Now you just have to make up the command line which is simple string
processing.  Probably define EXIMCMD as '/usr/bin/exim4 ' somewhere near
the top of the script as a pseudo constant.  Then you would make up the
command line with something like (NOTE: haven't tried this, just going
from memory and cursory look at my Python bible):

exim_cmd_line = EXIMCMD + '-Mvh ' + message_id
message_header = os.popen(exim_cmd_line)

You now have a file-like object message_header that you can use in the
script.

I hope this gets you on the right track.

Doug.


Ah, thank you. That put me on the right track. I did it a little differently, but you got me thinking in the right direction. I used os.system() as I need the output printed to screen so I can read email headers. Now on to the next step, to figure out a way to poll for stdin, pass that to exim, and process the message(thaw or remove it) based on what I read in the header.




Reply to: