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 相关文章推荐
简单解析Django框架中的表单验证
Jul 17 Python
详解Python中的相对导入和绝对导入
Jan 06 Python
python3.6+opencv3.4实现鼠标交互查看图片像素
Feb 26 Python
解决项目pycharm能运行,在终端却无法运行的问题
Jan 19 Python
python3.7 sys模块的具体使用
Jul 22 Python
python系统指定文件的查找只输出目录下所有文件及文件夹
Jan 19 Python
Python模块 _winreg操作注册表
Feb 05 Python
Tensorflow 多线程设置方式
Feb 06 Python
Django 实现将图片转为Base64,然后使用json传输
Mar 27 Python
python实现飞船游戏的纵向移动
Apr 24 Python
使用Keras预训练好的模型进行目标类别预测详解
Jun 27 Python
详解Python魔法方法之描述符类
May 26 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
德劲1103的维修打理经验
2021/03/02 无线电
Win2000+Apache+MySql+PHP4+PERL安装使用小结
2006/10/09 PHP
二十行语句实现从Excel到mysql的转化
2006/10/09 PHP
解析php中call_user_func_array的作用
2013/06/07 PHP
利用PHP+JS实现搜索自动提示(实例)
2013/06/09 PHP
PHP自动载入类文件函数__autoload的使用方法
2019/03/25 PHP
三级下拉菜单的js实现代码
2011/05/23 Javascript
根据经纬度计算地球上两点之间的距离js实现代码
2013/03/05 Javascript
javascript中的window.location.search方法简介
2013/09/02 Javascript
js 判断控件获得焦点的示例代码
2014/03/04 Javascript
JS实现的论坛Ajax打分效果完整实例
2015/10/31 Javascript
node.js 动态执行脚本
2016/06/02 Javascript
详解Node.js项目APM监控之New Relic
2017/05/12 Javascript
js导出Excel表格超出26位英文字符的解决方法ES6
2017/11/15 Javascript
第一个Vue插件从封装到发布
2017/11/22 Javascript
推荐10款扩展Web表单的JS插件
2017/12/25 Javascript
javascript设计模式 ? 装饰模式原理与应用实例分析
2020/04/14 Javascript
jQuery实现高度灵活的表单验证功能示例【无UI】
2020/04/30 jQuery
使用Typescript和ES模块发布Node模块的方法
2020/05/25 Javascript
原生js实现简单轮播图
2020/10/26 Javascript
深入理解Python变量与常量
2016/06/02 Python
Python定义一个跨越多行的字符串的多种方法小结
2018/07/19 Python
python高级特性和高阶函数及使用详解
2018/10/17 Python
Windows下Python3.6安装第三方模块的方法
2018/11/22 Python
python傅里叶变换FFT绘制频谱图
2019/07/19 Python
python读取与处理netcdf数据方式
2020/02/14 Python
CSS3 伪类选择器 nth-child()说明
2010/07/10 HTML / CSS
简单介绍HTML5中的文件导入
2015/05/08 HTML / CSS
HTML5新增加标签和功能概述
2016/09/05 HTML / CSS
英国鲜花递送:Blossoming Gifts
2020/07/10 全球购物
仓库管理制度
2014/01/21 职场文书
文秘档案管理岗位职责
2014/03/06 职场文书
写字楼租赁意向书
2014/07/30 职场文书
公司员工违纪检讨书
2015/05/05 职场文书
mysql优化之query_cache_limit参数说明
2021/07/01 MySQL
我们认为中短波广播场强仪的最佳组合
2022/04/05 无线电