ping 服务器并将其分配给 Python 中的变量后,如何获得完整响应?

How can I get the full response after pinging a server and assign it to a variable in Python?

我希望能够 ping 一个服务器,而不仅仅是返回 01,我希望能够看到如果 [=22] =] 它在您自己的终端中,例如能够看到数据包到达主机并返回所需的毫秒数。

我正在使用 Python 2.7 并且正在使用 Linux,这就是为什么在我的代码中我使用 -c 而不是 -n。这是我使用的代码:

import os
response = os.system("ping -c 1 8.8.8.8)
print response

您应该使用 subprocess.check_output:

https://docs.python.org/2/library/subprocess.html#subprocess.check_output