Muhammet Can
Muhammet Can

Reputation: 1354

Reading stdout from a running script with Python

I have some trouble with python. I am trying to get output from a running script and use it in another script. So, what I want to do is something like this;

Suppose the out.py and in.py like these;

# out.py
def main():
    while True:
        print "test"
        time.sleep(10)
main()

# in.py
text = sys.stdin.readline()
print "hello " + text

I want to run scripts like this,

$ python out.py | python in.py

but, this hangs and prints nothing instead printing "hello test" every 10 seconds. From this, I am guessing that | waits until the out.py terminates. All right then, what if I want to use in.py with a daemon?

My actual problem is; I'm trying to get output from arpwatch. arpwatch is running as daemon, and send ip/mac pairings to the stderr. I want to get that output and parse it; so, I need something like this;

$ arpwatch | python myscript.py

to achive this, how should i code myscript.py? I have tried to use subprocess module, but I had no success again. I hope I could clearly explain my problem since my English is decent.

Thanks for any help.

Upvotes: 3

Views: 418

Answers (1)

Fred Foo
Fred Foo

Reputation: 363567

The problem is one of buffering. It goes away if you explicitly flush stdout in out.py:

import sys
import time

def main():
    while True:
        print "test"
        sys.stdout.flush()
        time.sleep(10)
main()

Unfortunately, you'll have to rely on arpwatch doing this itself; if it doesn't, you'll want to try PTYs instead of pipes, since those count as terminals and will cause most applications to use line buffering instead of block buffering.

Upvotes: 4

Related Questions