我正在使用以下代碼來ping網站以檢查連接性。我如何解析結果以獲得「Lost =」來查看丟失了多少?使用Python和正則表達式解析windows命令結果
def pingTest():
host = "www.wired.com"
ping = subprocess.Popen(
["ping","-n","4",host],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out,error = ping.communicate()
print out
這是回報,我從出來
Pinging wired.com [173.223.232.42] with 32 bytes of data:
Reply from 173.223.232.42: bytes=32 time=54ms TTL=51
Reply from 173.223.232.42: bytes=32 time=54ms TTL=51
Reply from 173.223.232.42: bytes=32 time=54ms TTL=51
Reply from 173.223.232.42: bytes=32 time=54ms TTL=51
Ping statistics for 173.223.232.42:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 54ms, Maximum = 54ms, Average = 54ms
如果你也發佈了你從'out'得到的字符串,那麼你怎麼樣去打印它呢? – PaulMcG 2012-02-21 14:00:33
對不起 - 編輯! – lodkkx 2012-02-21 14:02:12