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中,可以方便地使用os模块来运行其他脚本或者程序,这样就可以在脚本中直接使用其他脚本...

python使用PyGame模块播放声音的方法

本文实例讲述了python使用PyGame模块播放声音的方法。分享给大家供大家参考。具体实现方法如下: import pygame pygame.init() pygame.mixe...

详解多线程Django程序耗尽数据库连接的问题

Django的ORM是非常好用的,哪怕不是做Web项目也值得一用,所以网上也可以找到不少使用 Django 开发非Web项目的资料,因为除了ORM之个,命令行、配置文件等组件也非常好用。...

寻找网站后台地址的python脚本

#!/usr/bin/python # This was written for educational purpose only. Use it at your own risk...

Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法

Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法

今天在网上copy的一段代码,代码很简单,每行看起来该缩进的都缩进了,运行的时候出现了如下错误:  【解决过程】  1.对于此错误,最常见的原因是,的确没有缩进。...