Need help with input redirection

Asun Friere afriere at yahoo.co.uk
Thu May 8 04:38:01 EDT 2003


defender <defender at austin.rr.com> wrote in message news:<3EB99F98.6020303 at austin.rr.com>...
> Greetings All:
> 
> I'm looking for a way to redirect input from one program into a python 
> program.  For example:
> 
> $tcpdump | pythonprog.py
> 
> The objective is to take the text output of one running program and pipe 
> it into the python program for parsing and processing.  It is important 
> during this process that I:
> 
 ...
> 
> This suggests a threading model, which I already know how to do.  The 
> piece I haven't figured out is how to get the input into the python 
> program in a continuous mode.  I'm assuming it has something to do with 
> sys.stdin?
> 

I think you are right.  A thread reading from sys.stdin would seem to
be the obvious way to go.  Putting good practice and threads to the
side for a moment, this seems to work fine :

$ cat >foo.py 
#!/usr/bin/env python
from time import localtime
while 1 :
    #some continuous output
    print localtime()
^D
$ cat > bar.py
#!/usr/bin/env python
import sys
while 1 :
    print sys.stdin.readline()
^D
$ chmod +x *py
$ foo.py | bar.py




More information about the Python-list mailing list