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实现文件按照日期命名的方法。分享给大家供大家参考。具体实现方法如下: 这里实现文件按照创建的时期批量重命名的功能 # -*- coding: utf-8 -...

使用python list 查找所有匹配元素的位置实例

如下所示: import re word = "test" s = "test abcdas test 1234 testcase testsuite" w = [m.start...

Python正则表达式常用函数总结

本文实例总结了Python正则表达式常用函数。分享给大家供大家参考,具体如下: re.match() 函数原型: match(pattern, string, flags=0) &nbs...

python微信撤回监测代码

 本文实例为大家分享了python微信撤回的监测代码,供大家参考,具体内容如下 注意:这里用了一个wechat库,当然,wechat库是基于微信提供的官方接口实现的。 这里的核...

Python实现二维曲线拟合的方法

如下所示: from numpy import * import numpy as np import matplotlib.pyplot as plt plt.close() f...