在linux系统下(w有效)(mac 下w无效):
实现pingIP地址的功能,-c1指发送报文一次,-w1指等待1秒
import requests,timefrom base64 import b64encodeimport osimport sysdef ping_baidu():ip = 'www.baidu.com'backinfo = os.system('ping -c 1 -w 1 %s'%ip)# print(backinfo)return backinfo'''res = ping_baidu()print('-'*100)print(res)print('-'*100)'''n = 1while 1:if ping_baidu() != 0:# 断网情况返回 512,联网情况返回 0time.sleep(5)n+=1print('-'*20,n,'-'*20)if log != 'connect':flog = open('log.txt','w')flog.write(str(log))
linux
Usage: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface][-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos][-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option][-w deadline] [-W timeout] [hop1 ...] destination
mac
usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize][-g sweepminsize] [-h sweepincrsize] [-i wait][-l preload] [-M mask | time] [-m ttl] [-p pattern][-S src_addr] [-s packetsize] [-t timeout][-W waittime][-z tos] hostping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait][-l preload] [-M mask | time] [-m ttl] [-p pattern] [-S src_addr][-s packetsize] [-T ttl] [-t timeout] [-W waittime][-z tos] mcast-groupApple specific options (to be specified before mcast-group or host like all options)-b boundif # bind the socket to the interface-k traffic_class # set traffic class socket option-K net_service_type # set traffic class socket options-apple-connect # call connect(2) in the socket-apple-time # display current time
