python test online
# -*- coding: utf-8 -*- import subprocess import re import time def check_online(ip_address): p = subprocess.Popen(["ping.exe", ip_address], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) out = p.stdout.read().decode('gbk') print(out) print(" ") reg_receive = 'TTL=\d\d' #reg_receive = "已接收 = \d" match_receive = re.search(reg_receive, out) receive_count = -1 ConnectFail='NetworkUnreachable' if match_receive: receive_count = int(match_receive.group()[4:]) #print(match_receive.group()) if receive_count > 0: #接受到的反馈大于0,表示网络通 print("*"*40) print('Network Reachable!') print("*"*40) print(" ") return 1 else: print("*"*40) print('Connect Fail!') print("*"*40) print(" ") return 0 #time.sleep(10) #check_online(ip_address) if __name__ == '__main__': ping_result = check_online("192.168.58.123")