在linux系统下(w有效)(mac 下w无效):
    实现pingIP地址的功能,-c1指发送报文一次,-w1指等待1秒

    1. import requests,time
    2. from base64 import b64encode
    3. import os
    4. import sys
    5. def ping_baidu():
    6. ip = 'www.baidu.com'
    7. backinfo = os.system('ping -c 1 -w 1 %s'%ip)
    8. # print(backinfo)
    9. return backinfo
    10. '''
    11. res = ping_baidu()
    12. print('-'*100)
    13. print(res)
    14. print('-'*100)
    15. '''
    16. n = 1
    17. while 1:
    18. if ping_baidu() != 0:
    19. # 断网情况返回 512,联网情况返回 0
    20. time.sleep(5)
    21. n+=1
    22. print('-'*20,n,'-'*20)
    23. if log != 'connect':
    24. flog = open('log.txt','w')
    25. flog.write(str(log))

    linux

    1. Usage: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
    2. [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
    3. [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
    4. [-w deadline] [-W timeout] [hop1 ...] destination

    mac

    1. usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize]
    2. [-g sweepminsize] [-h sweepincrsize] [-i wait]
    3. [-l preload] [-M mask | time] [-m ttl] [-p pattern]
    4. [-S src_addr] [-s packetsize] [-t timeout][-W waittime]
    5. [-z tos] host
    6. ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait]
    7. [-l preload] [-M mask | time] [-m ttl] [-p pattern] [-S src_addr]
    8. [-s packetsize] [-T ttl] [-t timeout] [-W waittime]
    9. [-z tos] mcast-group
    10. Apple specific options (to be specified before mcast-group or host like all options)
    11. -b boundif # bind the socket to the interface
    12. -k traffic_class # set traffic class socket option
    13. -K net_service_type # set traffic class socket options
    14. -apple-connect # call connect(2) in the socket
    15. -apple-time # display current time