Python获取当前公网ip并自动断开宽带连接实例代码
作者:mingz2013 时间:2021-08-28 12:40:27
今天写了一个获取当前公网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
基本的注释都有,大家可以自己参考。
来源:http://blog.csdn.net/mingzznet/article/details/52887470
标签:python,公网ip
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现学生成绩测评系统
2023-08-09 19:40:56
我的页面制作方法
2008-03-23 13:51:00
![](https://img.aspxhome.com/file/UploadPic/20083/23/2008323135325241s.png)
PHP Session变量不能传送到下一页的解决方法
2023-11-15 01:41:29
python编程实现12306的一个小爬虫实例
2021-09-23 10:58:14
![](https://img.aspxhome.com/file/2023/0/101650_0s.png)
整理Python中常用的conda命令操作
2022-07-17 16:53:33
![](https://img.aspxhome.com/file/2023/6/84986_0s.png)
Python 实现自动化Excel报表的步骤
2022-12-01 10:49:29
![](https://img.aspxhome.com/file/2023/2/70712_0s.jpg)
python钉钉机器人运维脚本监控实例
2022-08-23 22:19:48
![](https://img.aspxhome.com/file/2023/5/81855_0s.jpg)
解析Extjs与php数据交互(增删查改)
2023-07-19 09:06:42
web脚本程序攻击的防范
2007-10-18 13:29:00
网站构成的基本元素—网页布局
2008-01-04 09:49:00
Windows 64位下python3安装nltk模块
2023-11-20 09:42:12
![](https://img.aspxhome.com/file/2023/5/68275_0s.jpg)
matplotlib中legend位置调整解析
2023-07-17 03:00:44
![](https://img.aspxhome.com/file/2023/5/77625_0s.png)
python编程控制Android手机操作技巧示例
2021-12-01 07:59:05
![](https://img.aspxhome.com/file/2023/3/69553_0s.png)
Python数据分析之使用scikit-learn构建模型
2023-11-10 23:19:10
![](https://img.aspxhome.com/file/2023/5/64365_0s.jpg)
如何获取当前 select 元素的值
2010-03-29 13:07:00
从mysql到oracle你必须了解的50件事儿
2010-08-05 14:36:00
认清区别CSS的类class和id
2007-10-08 12:02:00
php 伪造本地文件包含漏洞的代码
2023-11-17 06:36:40
利用Django提供的ModelForm增删改数据的方法
2021-02-04 04:13:00
Mootools 1.2教程(23)——滑动效果(Slide)
2009-03-04 12:33:00