Python script monitoring another process
Jeffery D. Collins
jcollins at pacificnet.net
Thu Jan 27 14:32:17 EST 2000
Magnus Bjornsson wrote:
> Hi all,
>
> I'm new to Python so this is probably a typical newbie question...
> Anyway, I'm trying to build a Python script which does the following:
> 1. starts a process (that's easy)
> 2. checks the output from the process (from its stdout)
> 3. when a certain token is found in the process' output the script
> sends a new token to the process' stdin
>
> So, how can I redirect the process' stdout so I can manipulate it in the
> Python script? And how can I redirect my scripts stdout to the process'
> stdin?
> Using sys.stdin doesn't seem to do the trick since it doesn't affect the
> environment for the running process...
>
> Any help would be appreciated,
> - magnus e.
Here's a little code snippet...
- Jeff
# module proc.py
import sys
def looper():
fh = sys.stdout
while 1:
fh.write("something\n")
fh.write("<get input>\n")
fh.flush()
x = raw_input()
fh.write("you entered: " + x + '\n')
if __name__ == '__main__':
looper()
"""
>From the interpreter, run something like:
jdc:/jcollins% python
iPython 1.5.2+ (#9, Nov 27 1999, 12:41:03) [GCC 2.7.2.3] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import sys
>>> import popen2
>>> o,i = popen2.popen2("python proc.py")
>>> o.readline()
'something\012'
>>> o.readline()
'<get input>\012'
>>> i.write('jeff was here\n')
>>> i.flush()
>>> o.readline()
'you entered: jeff was here\012'
>>> o.readline()
'something\012'
>>> o.readline()
'<get input>\012'
>>>
"""
More information about the Python-list
mailing list