python监测当前联网状态并连接的实例

yipeiwu_com6年前Python基础

如下所示:

def test1():
 import os
 return1=os.system('ping 8.8.8.8')
 if return1:
  print 'ping fail'
  os.system('msdt.exe /id NetworkDiagnosticsNetworkAdapter')#调用系统网络诊断
 else:
  print 'ping'
def test2():
 import os
 import subprocess
 
 fnull = open(os.devnull, 'w')
 return1 = subprocess.call('ping 8.8.8.8', shell = True, stdout = fnull, stderr = fnull)
 if return1:
  return1 = subprocess.call('msdt.exe /id NetworkDiagnosticsNetworkAdapter', shell=True, stdout=fnull, stderr=fnull)
  print 'ping fail'
 else:
  print 'ping ok'
 fnull.close()
 
if __name__=='__main__':
 test2()

监测当前是否联网,没有时调用系统的联网监测;也算是原创吧,百度了下,功能都是分开的,综合到了一起。

以上这篇python监测当前联网状态并连接的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python针对给定列表中元素进行翻转操作的方法分析

Python针对给定列表中元素进行翻转操作的方法分析

本文实例讲述了Python针对给定列表中元素进行翻转操作的方法。分享给大家供大家参考,具体如下: 题目 给定一列表,翻转其中的元素,倒序输出 做法很简单,这里给出来两种做法,第一种最简单...

python集合用法实例分析

本文实例讲述了python集合用法。分享给大家供大家参考。具体分析如下: # sets are unordered collections of unique hashable el...

python选择排序算法实例总结

本文实例总结了python选择排序算法。分享给大家供大家参考。具体如下: 代码1: def ssort(V): #V is the list to be sorted j = 0...

通过Pandas读取大文件的实例

当数据文件过大时,由于计算机内存有限,需要对大文件进行分块读取: import pandas as pd f = open('E:/学习相关/Python/数据样例/用户侧数据/te...

Python2.7 实现引入自己写的类方法

系统环境:win10 开发环境:JetBrains PyCharm 2017.1.5 x64 Python版本:2.7 假如我们有一个class叫DBUtil,它在A.py里(最好一...