Python Forum
Send the output from a running script - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Send the output from a running script (/thread-2750.html)



Send the output from a running script - MeeranRizvi - Apr-06-2017

Hello Guyz,
Here i am writing a script by pinging an ip continously.



def ip_ping():
    import subprocess
    b= "172.30.36.136"
    a = subprocess.Popen(["ping","-t", b], stdout=subprocess.PIPE).stdout.read()
    if a.count("Destination host unreachable.")>5:
        print a
ip_ping()    
when the condition satisfies, it doesn't print the output to the idle shell while the script is running.
when i close the running script it gets printed in the Idle shell.
How to print the output while the script is running?

Thanks in Advance.


RE: Send the output from a running script - zivoni - Apr-06-2017

You need to read output repeatedly, example code with line reading:
import subprocess

def ip_ping(target):
    proc = subprocess.Popen(["ping", target], stdout=subprocess.PIPE)
    while True:
        line = proc.stdout.readline()
        print line

ip_ping("www.google.com")
Output:
PING www.google.com (216.58.212.228) 56(84) bytes of data. 64 bytes from ams16s22-in-f228.1e100.net (216.58.212.228): icmp_seq=1 ttl=58 time=0.995 ms   64 bytes from ams16s22-in-f228.1e100.net (216.58.212.228): icmp_seq=2 ttl=58 time=1.15 ms 64 bytes from ams16s22-in-f228.1e100.net (216.58.212.228): icmp_seq=3 ttl=58 time=0.642 ms
This is on linux, perhaps on windows it will work too. You can add your logic for counting into while block.