我想在我的hostname = "216.58.223.3 -c 4"代码行中添加-c 4标志
它目前的工作方式:
@staticmethod
def valve_southafrica_two():
print("Pinging Valve South Africa 2")
hostname = "155.133.238.163"
response = os.system("ping " + hostname)
if response == 0:
pingstatus = "Active"
else:
pingstatus = "Error"
print("Ping Test Complete")
return pingstatus我希望它的工作方式是这样的:
@staticmethod
def valve_southafrica_two():
print("Pinging Valve South Africa 2")
hostname = "155.133.238.163 -c 4"
response = os.system("ping " + hostname)
if response == 0:
pingstatus = "Active"
else:
pingstatus = "Error"
print("Ping Test Complete")
return pingstatus对于Linux +/ MacOS line,有没有办法在xxx.xxx (IP)旁边批量添加-c 4标志,或者手动在每一行旁边添加-c 4是唯一的解决方案?
发布于 2018-10-25 16:43:14
您可以使用字符串格式或如下所示的f字符串
flag = '-c 4'
# python 3.6
hostname = f'155.133.238.163 {flag}'
# python 2.7
hostname = '155.133.238.163 {0}'.format(flag)输出
155.133.238.163 -c 4
发布于 2018-10-25 16:48:28
通过它,我们可以将命令作为参数列表发送,因此我们只需要将-c 4指定为参数
import subprocess
#import os
def valve_southafrica_two():
print("Pinging Valve South Africa 2")
hostname = "155.133.238.163"
response =subprocess.call(["ping",hostname,"-c 4"])
if response == 0:
pingstatus = "Active"
else:
pingstatus = "Error"
print("Ping Test Complete")
return pingstatus
valve_southafrica_two()输出
Pinging Valve South Africa 2
PING 155.133.238.163 (155.133.238.163) 56(84) bytes of data.
64 bytes from 155.133.238.163: icmp_seq=1 ttl=47 time=327 ms
64 bytes from 155.133.238.163: icmp_seq=2 ttl=47 time=325 ms
64 bytes from 155.133.238.163: icmp_seq=3 ttl=47 time=326 ms
64 bytes from 155.133.238.163: icmp_seq=4 ttl=47 time=325 ms
--- 155.133.238.163 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 325.113/326.192/327.601/0.992 ms
Ping Test Completehttps://stackoverflow.com/questions/52984858
复制相似问题