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类参数self使用示例

    python类参数self使用示例

    python中__new__和__init__到底是怎么一回事,使用方法看下面的代码,大家参考使用吧
    2014-02-02
  • django admin 根据choice字段选择的不同来显示不同的页面方式

    django admin 根据choice字段选择的不同来显示不同的页面方式

    这篇文章主要介绍了django admin 根据choice字段选择的不同来显示不同的页面方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • Anaconda安装pytorch和paddle的方法步骤

    Anaconda安装pytorch和paddle的方法步骤

    本文主要介绍了Anaconda安装pytorch和paddle的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • pandas的to_datetime时间转换使用及学习心得

    pandas的to_datetime时间转换使用及学习心得

    这篇文章主要给大家介绍了关于pandas的to_datetime时间转换使用及学习心得的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用pandas具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • Pyramid将models.py文件的内容分布到多个文件的方法

    Pyramid将models.py文件的内容分布到多个文件的方法

    默认的Pyramid代码结构中,就只有一个models.py文件,在实际项目中,如果需要对models进行分类,放到不同文件下,应该怎么办
    2013-11-11
  • python 获取谷歌浏览器保存的密码

    python 获取谷歌浏览器保存的密码

    这篇文章主要介绍了python 获取谷歌浏览器保存的密码的示例代码,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2021-01-01
  • python基于Pandas读写MySQL数据库

    python基于Pandas读写MySQL数据库

    这篇文章主要介绍了python基于Pandas读写MySQL数据库,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-04-04
  • python获取点击的坐标画图形的方法

    python获取点击的坐标画图形的方法

    今天小编就为大家分享一篇python获取点击的坐标画图形的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • 详解Python中命令行参数argparse的常用命令

    详解Python中命令行参数argparse的常用命令

    这篇文章主要为大家详细介绍了Python中命令行参数argparse的一些常用命令,文中的示例代码讲解详细,具有一定的学习价值,需要的可以了解一下
    2023-01-01
  • python编码问题汇总

    python编码问题汇总

    这篇文章主要给大家分享的是python编码问题汇总,字符编码简单介绍和发展史及使用方法的一些介绍,文章内容详细,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-03-03

最新评论