Python实现获取当前公网ip并且自动断开宽带连接功能

来源:互联网 发布:淘宝网高档丝巾 编辑:程序博客网 时间:2024/05/16 18:49

http://blog.csdn.net/jinmmd/article/details/6997381

今天写了一个获取当前公网ip并且自动断开宽带连接的文件,和大家分享下。

这个文件的具体用途大家懂的,可以尽管拿去用,不过目前只适用于Windows平台,我的Python版本是2.7的,win32ras模块需要下载pywin32。

代码如下:

[python] view plain copy
  1. #!coding: cp936  
  2. import win32ras  
  3. import time,os  
  4.   
  5. def Connect(dialname, account, passwd):  
  6.     dial_params = (dialname, '''', account, passwd, '')  
  7.     return win32ras.Dial(NoneNone, dial_params, None)  
  8.   
  9. def DialBroadband():  
  10.     dialname = '宽带连接'  #just a name  
  11.     account = '********'  
  12.     passwd = '****************'  
  13.     try:  
  14.         #handle is a pid, for disconnect or showipadrress, if connect success return 0.  
  15.         #account is the username that your ISP supposed, passwd is the password.  
  16.         handle, result = Connect(dialname, account, passwd)  
  17.         if result == 0:  
  18.             print "Connection success!"  
  19.             return handle, result  
  20.         else:  
  21.             print "Connection failed, wait for 5 seconds and try again..."  
  22.             time.sleep(5)  
  23.             DialBroadband()      
  24.     except:  
  25.         print "Can't finish this connection, please check out."  
  26.         return  
  27.   
  28. def Disconnect(handle):  
  29.     if handle != None:  
  30.         try:  
  31.             win32ras.HangUp(handle)  
  32.             print "Disconnection success!"  
  33.             return "success"  
  34.         except:  
  35.             print "Disconnection failed, wait for 5 seconds and try again..."  
  36.             time.sleep(5)  
  37.             Disconnect()  
  38.     else:  
  39.         print "Can't find the process!"  
  40.         return  
  41.   
  42. def Check_for_Broadband():  
  43.     connections = []  
  44.     connections = win32ras.EnumConnections()  
  45.     if(len(connections) == 0):  
  46.         print "The system is not running any broadband connection."  
  47.         return  
  48.     else:  
  49.         print "The system is running %d broadband connection." % len(connections)  
  50.         return connections  
  51.   
  52. def ShowIpAddress(handle):  
  53.     print win32ras.GetConnectStatus(handle)  
  54.     data = os.popen("ipconfig","r").readlines()  
  55.     have_ppp = 0  
  56.     ip_str = None  
  57.     for line in data:  
  58.         if line.find("宽带连接")>=0:  
  59.             have_ppp = 1  
  60.         #if your system language is English, you should write like this:  
  61.         #if have_ppp and line.strip().startswith("IP Address"):  
  62.         #in othewords, replace the "IPv4 地址" to "IP Address"  
  63.         if have_ppp and line.strip().startswith("IPv4 地址"):  
  64.             ip_str = line.split(":")[1].strip()  
  65.             have_ppp = 0  
  66.             print ip_str  
  67.   
  68. #get my ipaddress anf disconnect broadband connection.  
  69. def main():  
  70.     data = Check_for_Broadband()  
  71.     #if exist running broadband connection, disconnected it.  
  72.     if data != None:  
  73.         for p in data:  
  74.             ShowIpAddress(p[0])  
  75.             if(Disconnect(p[0]) == "success"):  
  76.                 print "%s has been disconnected." % p[1]  
  77.             time.sleep(3)  
  78.     else:  
  79.         pid, res = DialBroadband()  
  80.         ShowIpAddress(pid)  
  81.         time.sleep(3)  
  82.         Disconnect(pid)  
  83.     return "finsh test"  
  84.   
  85. test = main()  
  86. print test  

基本的注释都有了,欢迎批评指点。

0 0