(使用 Windows 10)
考虑使用subprocess 模块,如下所示:
>>> import os, subprocess
>>> startupinfo = subprocess.STARTUPINFO()
>>> startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
>>> with subprocess.Popen("ping 192.168.1.1", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
for line in p.stdout:
print(line, end="")
Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Ping statistics for 192.168.1.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
>>> with subprocess.Popen("ping 10.1.10.1", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
for line in p.stdout:
print(line, end="")
Pinging 10.1.10.1 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 10.1.10.1:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
遍历p.stdout 以获取每行结果作为字符串。显然,如果您愿意,您可以忽略这一点,在这种情况下,可以删除 text 和 stdout 参数。
使用p.returncode获取返回码。
在我的 Windows 10 机器上,ping 在收到回复时返回 0,如果出现错误则返回 1。请注意,当我在尝试 ping 我的 LAN 上不存在的地址后收到 Destination host unreachable 响应时,它会返回 0。虽然我怀疑这对您的用例来说是个问题。
p.returncode 将返回 None 如果您在进程结束之前访问它。
您可以通过调用p.wait() 方法等待进程结束,如下所示:
with subprocess.Popen("ping 192.168.1.1", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
p.wait()
print("Return code for 192.168.1.1:", p.returncode)
for line in p.stdout:
print(line, end="") # This will now all print immediately, after waiting for the process to finish
输出:
Return code for 192.168.1.1: 0
Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Ping statistics for 192.168.1.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
或者您可以在with 语句之后访问p.returncode,如下所示:
with subprocess.Popen("ping 10.1.10.1", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
for line in p.stdout:
print(line, end="")
print("Return code for 10.1.10.1:", p.returncode)
输出:
Pinging 10.1.10.1 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 10.1.10.1:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Return code for 10.1.10.1: 1
在我的 LAN 上 Ping 一个不存在的地址返回 0:
with subprocess.Popen("ping 192.168.1.99", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
for line in p.stdout:
print(line, end="")
print("Return code for 192.168.1.99:", p.returncode)
输出:
Pinging 192.168.1.99 with 32 bytes of data:
Reply from 192.168.1.105: Destination host unreachable.
Reply from 192.168.1.105: Destination host unreachable.
Reply from 192.168.1.105: Destination host unreachable.
Reply from 192.168.1.105: Destination host unreachable.
Ping statistics for 192.168.1.99:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Return code for 192.168.1.99: 0
ping 一个无效的 URL 返回 1:
with subprocess.Popen("ping www..com", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
for line in p.stdout:
print(line, end="")
print("Return code for www..com:", p.returncode)
输出:
Ping request could not find host www..com. Please check the name and try again.
Return code for www..com: 1