Python获取当前公网ip并自动断开宽带连接实例代码


Posted in Python onJanuary 12, 2018

今天写了一个获取当前公网ip并且自动断开宽带连接的文件,和大家分享下。

这个文件的具体用途大家懂的,可以尽管拿去用,不过目前只适用于Windows平台,我的Python版本是2.7的,win32ras模块需要下载pywin32。

代码如下:

#!coding: cp936 
import win32ras 
import time,os 
 
def Connect(dialname, account, passwd): 
  dial_params = (dialname, '', '', account, passwd, '') 
  return win32ras.Dial(None, None, dial_params, None) 
 
def DialBroadband(): 
  dialname = '宽带连接' #just a name 
  account = '********' 
  passwd = '****************' 
  try: 
    #handle is a pid, for disconnect or showipadrress, if connect success return 0. 
    #account is the username that your ISP supposed, passwd is the password. 
    handle, result = Connect(dialname, account, passwd) 
    if result == 0: 
      print "Connection success!" 
      return handle, result 
    else: 
      print "Connection failed, wait for 5 seconds and try again..." 
      time.sleep(5) 
      DialBroadband()   
  except: 
    print "Can't finish this connection, please check out." 
    return 
 
def Disconnect(handle): 
  if handle != None: 
    try: 
      win32ras.HangUp(handle) 
      print "Disconnection success!" 
      return "success" 
    except: 
      print "Disconnection failed, wait for 5 seconds and try again..." 
      time.sleep(5) 
      Disconnect() 
  else: 
    print "Can't find the process!" 
    return 
 
def Check_for_Broadband(): 
  connections = [] 
  connections = win32ras.EnumConnections() 
  if(len(connections) == 0): 
    print "The system is not running any broadband connection." 
    return 
  else: 
    print "The system is running %d broadband connection." % len(connections) 
    return connections 
 
def ShowIpAddress(handle): 
  print win32ras.GetConnectStatus(handle) 
  data = os.popen("ipconfig","r").readlines() 
  have_ppp = 0 
  ip_str = None 
  for line in data: 
    if line.find("宽带连接")>=0: 
      have_ppp = 1 
    #if your system language is English, you should write like this: 
    #if have_ppp and line.strip().startswith("IP Address"): 
    #in othewords, replace the "IPv4 地址" to "IP Address" 
    if have_ppp and line.strip().startswith("IPv4 地址"): 
      ip_str = line.split(":")[1].strip() 
      have_ppp = 0 
      print ip_str 
 
#get my ipaddress anf disconnect broadband connection. 
def main(): 
  data = Check_for_Broadband() 
  #if exist running broadband connection, disconnected it. 
  if data != None: 
    for p in data: 
      ShowIpAddress(p[0]) 
      if(Disconnect(p[0]) == "success"): 
        print "%s has been disconnected." % p[1] 
      time.sleep(3) 
  else: 
    pid, res = DialBroadband() 
    ShowIpAddress(pid) 
    time.sleep(3) 
    Disconnect(pid) 
  return "finsh test" 
 
test = main() 
print test

基本的注释都有,大家可以自己参考。

总结

以上就是本文关于Python获取当前公网ip并自动断开宽带连接实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
对Python3 * 和 ** 运算符详解
Feb 16 Python
python使用pymongo操作mongo的完整步骤
Apr 13 Python
详解Python3 基本数据类型
Apr 19 Python
python的set处理二维数组转一维数组的方法示例
May 31 Python
对Django中static(静态)文件详解以及{% static %}标签的使用方法
Jul 28 Python
django 使用 PIL 压缩图片的例子
Aug 16 Python
Python selenium模拟手动操作实现无人值守刷积分功能
May 13 Python
python实现猜数游戏(保存游戏记录)
Jun 22 Python
使用matplotlib的pyplot模块绘图的实现示例
Jul 12 Python
Python3爬虫关于识别点触点选验证码的实例讲解
Jul 30 Python
Python + opencv对拍照得到的图片进行背景去除的实现方法
Nov 18 Python
python开发实时可视化仪表盘的示例
May 07 Python
python SSH模块登录,远程机执行shell命令实例解析
Jan 12 #Python
python opencv实现任意角度的透视变换实例代码
Jan 12 #Python
Python数字图像处理之霍夫线变换实现详解
Jan 12 #Python
Python实现霍夫圆和椭圆变换代码详解
Jan 12 #Python
微信跳一跳python自动代码解读1.0
Jan 12 #Python
Tornado 多进程实现分析详解
Jan 12 #Python
快速了解Python相对导入
Jan 12 #Python
You might like
php 全文搜索和替换的实现代码
2008/07/29 PHP
Windows 下的 PHP-PEAR 安装方法
2010/11/20 PHP
PHP写的求多项式导数的函数代码
2012/07/04 PHP
解析php中用PHPMailer来发送邮件的示例(126.com的例子)
2013/06/24 PHP
PHP生成plist数据的方法
2015/06/16 PHP
PHP判断文件是否被引入的方法get_included_files用法示例
2016/11/29 PHP
如何通过View::first使用Laravel Blade的动态模板详解
2017/09/21 PHP
Nigma vs Liquid BO3 第二场2.14
2021/03/10 DOTA
点击标签切换和自动切换DIV选项卡
2014/08/10 Javascript
js判断是否按下了Shift键的方法
2015/01/27 Javascript
Javascript中常用类型的格式化方法小结
2016/12/26 Javascript
Vue 页面跳转不用router-link的实现代码
2018/04/12 Javascript
Vue加载组件、动态加载组件的几种方式
2018/08/31 Javascript
使用jquery Ajax实现上传附件功能
2018/10/23 jQuery
微信小程序之数据绑定原理解析
2019/08/14 Javascript
vue项目中常见问题及解决方案(推荐)
2019/10/21 Javascript
vue中根据时间戳判断对应的时间(今天 昨天 前天)
2019/12/20 Javascript
微信小程序吸底区域适配iPhoneX的实现
2020/04/09 Javascript
vue解决跨域问题(推荐)
2020/11/10 Javascript
[00:12]DAC2018 no[o]ne亮相SOLO赛 他是否如他的id一样无人可挡?
2018/04/06 DOTA
学习python (2)
2006/10/31 Python
Sanic框架配置操作分析
2018/07/17 Python
pygame实现简易飞机大战
2018/09/11 Python
python 接收处理外带的参数方法
2018/12/03 Python
python实现飞机大战游戏
2020/10/26 Python
浅谈spring boot 集成 log4j 解决与logback冲突的问题
2020/02/20 Python
用python打开摄像头并把图像传回qq邮箱(Pyinstaller打包)
2020/05/17 Python
Python改变对象的字符串显示的方法
2020/08/01 Python
欧洲最大的婴幼儿服装及内衣公司:Petit Bateau(小帆船)
2016/08/16 全球购物
英国领先的野生鸟类食品供应商:GardenBird
2018/08/09 全球购物
当文件系统受到破坏时,如何检查和修复系统?
2012/03/09 面试题
应届大学生简历中的自我评价
2014/01/15 职场文书
2015秋季运动会通讯稿
2015/07/18 职场文书
幼儿园大班教育随笔
2015/08/14 职场文书
导游词幽默开场白
2019/06/26 职场文书
关于springboot配置druid数据源不生效问题(踩坑记)
2021/09/25 Java/Android