[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

On Mon, Sep 03, 2007 at 01:58:59PM -0700, Freddy Freeloader wrote:
> Douglas A. Tutty wrote:
> >On Mon, Sep 03, 2007 at 11:48:48AM -0700, Freddy Freeloader wrote:
> >  
> >>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.
> >
> >Before we get too far down the garden path with this, what is the big
> >issue?  I'm on dialup.  I've never had to deal with removing messages
> >from the queue since I have exim run with -qqff which automatically
> >thaws the list every time it runs.  After enough retries, exim returns
> >the message to sender.  Then again, I'm going to a smarthost.
> >
> >Under what circumstances would you need to manually remove a message
> >from the queue?
> >
> >As for polling stdin, why?  What will it get from stdin?
> Mainly the issue is learning to use python. 
> What I want the script to read from stdin in will be my choice to either 
> thaw the message or remove it from the queue.  And, yes, I know Exim 
> will delete anything in the queue after so many tries to deliver it.   
> This is more a learning project than anything else at this point.  I'm a 
> newbie exim admin and a newbie to python, and I'm just wanting to make 
> sure all frozen messages have nothing to do my acl's, and learn 
> something about python at the same time. 
> So, if you don't like my dinky little Python project all I can say is, 
> stop replying.  It's my time, and my energy being put into it.  If you 
> don't want to help any further, don't.  I'll get my answers elsewhere, 
> and there will be no hard feelings on my part....

I'll help this evening (then I'm away for a while).  I just wasn't sure
what you meant by polling stdin; I thought that perhaps some other
process was going to give you the message ID that the script was to

OK.  So you run your script and it gets the list of frozen messages and
runs exim4 -Mvh [message-id] which prints out the headers to your
screen.  Now you just need python to ask what you want to do: [T]haw the
message, [R]emove the message, or [D]o nothing.  It is vastly easier to
get the input as a string where the user would type the choice letter
and hit enter than it is to capture the choice letter by itself.

Something like this:

raw_choice = raw_input('[T]thaw the message, [R]emove the message, or [D]o
nothing?  Enter choice: ')

Now you need to massage raw_choice:

remove whitespace (in case they hit space before a choice letter)

choice = raw_choice.strip()

test only the first char:

choice = raw_choice.strip()[0]

test only the first char as upper case:

choice = raw_choice.strip()[0].upper()

Of course, you could combine all this:

choice = raw_input('[T]haw the message, [R]emove the message, or [D]o
nothing? Enter choice: ').strip()[0].upper()

However, that can look a little daunting in the middle of the night.  I
tend to break it up into getting the user's input, cleaning it up,
testing it, then respond to it.

Define a list of valid choices.  Since strings are already sequence

valid_choices = 'TRD'

Wrap it in a while (1) loop to test for a valid choice:

if choice in valid_choices:

Then decide if you want to trap exceptions such as Ctrl-C
(KeyboardInterrupt).  If so, wrap it in a try block and decide how to
handle it.

The joy of all this is, you can get the basics working, then add
refinements rather than doing it all at once then having a problem.


I have the Python 2.1 Bible.  Its great.  There may be other great
python books out since.  That plus the python docs package are all I've
ever needed.

When I have a programming problem, I tend to write it out in rough
python as a pseudocode.  However, unless you already know python or
another language, it may be easier to use a flow-chart.  This is one of
the few times that I still recommend flow charts.



Reply to: