python调用命令并获取实时输出

python

from subprocess import *

import datetime,time

import threading

import os,sys

def runcmd():

cmd = ["ping","-t","www.baidu.com"]

r=Popen(cmd,stdin=PIPE,stdout=PIPE,stderr=PIPE)

for i in iter(r.stdout.readline,b""):

print(i.decode("gbk").strip())

child = threading.Thread(target=runcmd)

child.daemon=True

child.start()

num = 0

timeout = 30

while True:

time.sleep(1)

num += 1

print("Now num ",num)

if num == timeout:

os._exit(0)

Now num  28

来自 112.80.248.75 的回复: 字节=32 时间=20ms TTL=55

Now num 29

来自 112.80.248.75 的回复: 字节=32 时间=10ms TTL=55

Now num 30

以上是 python调用命令并获取实时输出 的全部内容, 来源链接: utcz.com/z/387436.html

回到顶部