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 相关文章推荐
Python统计日志中每个IP出现次数的方法
Jul 06 Python
python爬虫入门教程--利用requests构建知乎API(三)
May 25 Python
python实现决策树、随机森林的简单原理
Mar 26 Python
python 定时修改数据库的示例代码
Apr 08 Python
Python图像处理实现两幅图像合成一幅图像的方法【测试可用】
Jan 04 Python
python多线程下信号处理程序示例
May 31 Python
python连接mongodb集群方法详解
Feb 13 Python
在python tkinter界面中添加按钮的实例
Mar 04 Python
python实现对变位词的判断方法
Apr 05 Python
Python xml、字典、json、类四种数据类型如何实现互相转换
May 27 Python
python中opencv实现图片文本倾斜校正
Jun 11 Python
Python MNIST手写体识别详解与试练
Nov 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读取3389的脚本
2014/05/06 PHP
php提交表单时保留多个空格及换行的文本样式的方法
2017/06/20 PHP
JS对URL字符串进行编码/解码分析
2008/10/25 Javascript
JavaScript中函数声明优先于变量声明的实例分析
2012/03/01 Javascript
JavaScript 高级篇之函数 (四)
2012/04/07 Javascript
js中escape对应的C#解码函数 UrlDecode
2012/12/16 Javascript
Javascript自定义排序 node运行 实例
2013/06/05 Javascript
判断iframe里的页面是否加载完成
2014/06/06 Javascript
JavaScript如何获取数组最大值和最小值
2015/11/18 Javascript
js实现卡片式项目管理界面UI设计效果
2015/12/08 Javascript
JavaScript事件 "事件对象"的注意要点
2016/01/14 Javascript
jquery获取点击控件的绝对位置简单实例
2016/10/13 Javascript
Thinkjs3新手入门之如何使用静态资源目录
2017/12/06 Javascript
javascript代码优化的8点总结
2018/01/29 Javascript
Vue动态加载异步组件的方法
2018/11/21 Javascript
layui监听单元格编辑前后交互的例子
2019/09/16 Javascript
解决layui数据表格排序图标被超出的表头挤出去的问题
2019/09/19 Javascript
[43:35]EG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
Python黑魔法Descriptor描述符的实例解析
2016/06/02 Python
python导入时小括号大作用
2017/01/10 Python
Python把csv数据写入list和字典类型的变量脚本方法
2018/06/15 Python
如何优雅地处理Django中的favicon.ico图标详解
2018/07/05 Python
Python类的继承、多态及获取对象信息操作详解
2019/02/28 Python
Django web框架使用url path name详解
2019/04/29 Python
深入浅析python3中的unicode和bytes问题
2019/07/03 Python
django项目中使用手机号登录的实例代码
2019/08/15 Python
python中requests模拟登录的三种方式(携带cookie/session进行请求网站)
2020/11/17 Python
HTML5 Canvas draw方法制作动画效果示例
2013/07/11 HTML / CSS
全球性的在线时尚男装零售商:boohooMAN
2016/12/17 全球购物
比利时香水网上商店:NOTINO
2018/03/28 全球购物
见习期自我鉴定
2013/11/07 职场文书
社区党务公开实施方案
2014/03/18 职场文书
小学生评语大全
2014/04/18 职场文书
优秀会计求职信
2014/07/04 职场文书
2015年档案室工作总结
2015/05/23 职场文书
Python基础之数据类型知识汇总
2021/05/18 Python