因此,每當我發(fā)送數(shù)據(jù)包時,我一直在努力抑制終端輸出。我只想驗證響應(yīng)(0,2 或其他),這樣我的終端就不會收到標準的“ping 統(tǒng)計信息、收到的數(shù)據(jù)包、數(shù)據(jù)包丟失”的垃圾郵件。我將如何在代碼方面進行此操作?我不是在尋找終端/bash“方式”。def ping(): hosts = open('hosts.txt', 'r') for host_ip in hosts: res = subprocess.call(['ping', '-c', '1', host_ip]) if res == 0: print "ping to", host_ip, "OK" elif res == 2: print "no response from", host_ip else: print "ping to", host_ip, "failed!"
1 回答

牛魔王的故事
TA貢獻1830條經(jīng)驗 獲得超3個贊
我只是使用 python 的subprocess.Popen類ping 到 google dns 服務(wù)器,除了我在代碼中打印的返回碼之外,它什么都不返回到終端
import subprocess
process = subprocess.Popen(['ping','-c' ,'1', '8.8.8.8'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
returncode = process.returncode
print(returncode)
輸出
0
添加回答
舉報
0/150
提交
取消