• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

python3 使用代码 ping 服务器

开发技术 开发技术 1天前 4次浏览

import subprocess as sp
url = 'www.baidu.com'
status, result = sp.getstatusoutput("ping " + url + " -w 2000")
print(status)
print(result)
if "请求超时" in result:
print('hah')

!/usr/bin/python3
Windos
import subprocess as sp
ip =[‘10.168.10.161′,’10.168.10.209′,’10.168.10.54′,’10.168.10.55′,’10.168.10.123’]
good = 0
bad = 0
count = 0
badIp=[]
for url in ip:
status, result = sp.getstatusoutput(“ping ” + url + ” -w 2000″)
count+=1
if “请求超时” in result:
bad+=1
badIp.append(url)
else:
good +=1
print(badIp)
print(‘总共ping了 %d条ip’%(count))
print(‘其中%d条有问题’%(bad))
print(‘%d条没问题’%(good))
print(‘坏ip是:’)
print(badIp)

LINUX
import os
hostname =”google.com” #example
response = os.system(“ping ” + hostname)

and then check the response…
if response == 0:
print hostname, ‘is up!’
else:
print hostname, ‘is down!’


程序员灯塔
转载请注明原文链接:python3 使用代码 ping 服务器
喜欢 (0)