3

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for such a quick reply. works like a charm :) i'll look at pty's for arpwatch.
@MuhammetCan: you're welcome. Forget what I said about pexpect, you won't need that when you're only reading a process's stdout; the pty module will suffice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.