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中使用strip()方法删除字符串中空格的教程
May 20 Python
python字符串对其居中显示的方法
Jul 11 Python
在Apache服务器上同时运行多个Django程序的方法
Jul 22 Python
Python如何通过subprocess调用adb命令详解
Aug 27 Python
python计算列表内各元素的个数实例
Jun 29 Python
解决python "No module named pip" 的问题
Oct 13 Python
详解python tkinter教程-事件绑定
Mar 28 Python
python将excel转换为csv的代码方法总结
Jul 03 Python
Python3实现mysql连接和数据框的形成(实例代码)
Jan 17 Python
python的列表List求均值和中位数实例
Mar 03 Python
Pycharm编辑器功能之代码折叠效果的实现代码
Oct 15 Python
Python第三方库安装缓慢的解决方法
Feb 06 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简单封装了一些常用JS操作
2007/02/25 PHP
php写的带缓存数据功能的mysqli类
2012/09/06 PHP
优化PHP代码技巧的小结
2013/06/02 PHP
php的webservice的wsdl的XML无法显示问题的解决方法
2014/03/11 PHP
可以保证单词完整性的PHP英文字符串截取代码分享
2014/07/15 PHP
PHP实现的oracle分页函数实例
2016/01/25 PHP
Thinkphp批量更新数据的方法汇总
2016/06/29 PHP
PHP使用文件锁解决高并发问题示例
2018/03/29 PHP
laravel + vue实现的数据统计绘图(今天、7天、30天数据)
2018/07/31 PHP
php基于协程实现异步的方法分析
2019/07/17 PHP
PHP超全局变量实现原理及代码解析
2020/09/01 PHP
jQuery EasyUI API 中文文档 - ValidateBox验证框
2011/10/06 Javascript
一个简单的Node.js异步操作管理器分享
2014/04/29 Javascript
JQuery创建DOM节点的方法
2015/06/11 Javascript
nodejs实现bigpipe异步加载页面方案
2016/01/26 NodeJs
VUE实现可随意拖动的弹窗组件
2018/09/25 Javascript
JavaScript继承与聚合实例详解
2019/01/22 Javascript
vue-router 路由传参用法实例分析
2020/03/06 Javascript
下载糗事百科的内容_python版
2008/12/07 Python
Python实现批量转换文件编码的方法
2015/07/28 Python
Django框架搭建的简易图书信息网站案例
2019/05/25 Python
python ctypes库2_指定参数类型和返回类型详解
2019/11/19 Python
TensorFlow使用Graph的基本操作的实现
2020/04/22 Python
python爬虫可以爬什么
2020/06/16 Python
Python爬虫抓取指定网页图片代码实例
2020/07/24 Python
降低python版本的操作方法
2020/09/11 Python
利用Opencv实现图片的油画特效实例
2021/02/28 Python
美国在线旅行社:Crystal Travel
2018/09/11 全球购物
寻找完美的房车租赁:RVShare
2019/02/23 全球购物
测绘工程专业个人自我评价
2013/12/01 职场文书
《莫高窟》教学反思
2014/02/25 职场文书
领导班子四风问题对照检查材料
2014/09/27 职场文书
物价局领导班子四风问题整改措施
2014/10/26 职场文书
消防隐患整改通知书
2015/04/22 职场文书
乒乓球比赛通知
2015/04/27 职场文书
施工安全协议书
2016/03/22 职场文书