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

 更新时间:2018年01月12日 15:33:58   作者:mingz2013  
这篇文章主要介绍了Python获取当前公网ip并自动断开宽带连接实例代码,具有一定借鉴价值,需要的朋友可以参考下

今天写了一个获取当前公网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 改变数组类型为uint8的实现

    Python 改变数组类型为uint8的实现

    这篇文章主要介绍了Python 改变数组类型为uint8的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Python中打印详细堆栈信息的技巧分享

    Python中打印详细堆栈信息的技巧分享

    在 Python 开发过程中,调试是一个不可或缺的环节,当代码出现问题时,能够快速准确地定位问题所在是提高开发效率的关键,堆栈信息作为程序执行过程中的调用记录,对于理解程序的运行状态和定位错误至关重要,需要的朋友可以参考下
    2024-11-11
  • Pycharm学习教程(5) Python快捷键相关设置

    Pycharm学习教程(5) Python快捷键相关设置

    这篇文章主要为大家详细介绍了最全的Pycharm学习教程第五篇,Python快捷键相关设置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Python基础之标准库和常用的第三方库案例教程

    Python基础之标准库和常用的第三方库案例教程

    这篇文章主要介绍了Python基础之标准库和常用的第三方库案例教程,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • Python实现的简单模板引擎功能示例

    Python实现的简单模板引擎功能示例

    这篇文章主要介绍了Python实现的简单模板引擎功能,结合具体实例形式分析了Python模版引擎的定义与使用方法,需要的朋友可以参考下
    2017-09-09
  • mac在matplotlib中显示中文的操作方法

    mac在matplotlib中显示中文的操作方法

    这篇文章主要介绍了mac如何在matplotlib中显示中文,本文分步骤给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • 在python中解决死锁的问题

    在python中解决死锁的问题

    这篇文章主要介绍了在python中解决死锁的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • Python 如何实时向文件写入数据(附代码)

    Python 如何实时向文件写入数据(附代码)

    这篇文章主要介绍了Python 如何实时向文件写入数据(附代码),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Python的Flask框架中@app.route的用法教程

    Python的Flask框架中@app.route的用法教程

    这篇文章主要介绍了Python的Flask框架中@app.route的用法教程,包括相关的正则表达式讲解,是Flask学习过程当中的基础知识,需要的朋友可以参考下
    2015-03-03
  • python实现图像降噪

    python实现图像降噪

    这篇文章主要为大家详细介绍了python实现图像降噪,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08

最新评论