subprocess.call()输出不会改变

时间:2019-02-27 15:28:48

标签: python

我试图使用subprocess.call(['ping', '-n', '3','-w', '100', PingAddress]) ping IP,但是输出不会从0或1更改。我正在ping开关以查看其是否在线。如果我连接到它,它将输出1,依此类推。但是,如果我已连接,则断开连接后仍会输出1

ping = subprocess.call(['ping', '-n', '3','-w', '100', PingAddress])
while True:
    ping 
    print(ping)
    if ping != 0 and t < 3:
        t = t + 1
    elif ping == 0: #Success
        t = 0
        print(test)
    else:
        print(test)
        while True: 
            ping
            print(ping)
            if ping == 0:
                do stuff

1 个答案:

答案 0 :(得分:2)

ping = subprocess.call(['ping', '-n', '3','-w', '100', PingAddress])
while True:
    ping 

您仅调用一次subprocess.callping变量的值在此代码中不再更改。

尝试一下:

while True:
    ping = subprocess.call(['ping', '-n', '3','-w', '100', PingAddress])
    ...