Python获取当前公网ip并自动断开宽带连接实例代码
作者:mingz2013 发布时间:2021-08-28 12:40:27
标签: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
基本的注释都有,大家可以自己参考。
来源:http://blog.csdn.net/mingzznet/article/details/52887470


猜你喜欢
- 用js判断复选框是否选中及选中个数,以前经常有朋友提到复选框的选中问题,这段代码可以帮你解决这个问题,而且它还可以判断你选择了多少个复选框,
- 引言通常,您可能希望在 Pandas DataFrame 中插入一个新列。幸运的是,使用 pandas insert()函数很容易做到这一点
- 前言前几天去我姐家里蹭饭吃,发现我姐家里的小外甥女儿已经上小学了,正在疯狂赶作业,加减乘除还有很多题目都经常出错。我姐说老师每天给他们布置了
- 一、新建项目,在主配置文件中,修改以下内容:ALLOWED_HOSTS = ['127.0.0.1','localh
- 一、操作redisredis是一个key-value存储系统,value的类型包括string(字符串),list(链表),set(集合),
- 1.1全部php生成结构1.2html中嵌套php总结如下:html和php混写规则:php代码必须包在<?php ?>html
- 方法: 使用urlencode函数urllib.request.urlopen()import urllib.requestimport u
- 看了山人表单验证,又看了其他一些验证程序和相关的一些参考资料,写出了一个比较简洁的js表单验证程序。特点是扩展容易,可以方便的添加自己需要的
- 比如做一些高级的配置,还比如在客户端或远程执行数据库操作等工作时,ISQL是很方便的。比如入侵一台有SQL服务的主机,入侵SQL Serve
- 事件背景过年在家正好闲得没有太多事情,想起年前一个研发项目负责人反馈的问题:“老李啊,我们组一直在使用你这边的 gin
- 众所周知,python是写爬虫的利器,今天作者用python写一个小爬虫爬下一个段子网站的众多段子。目标段子网站为“http://ishuo
- 有没有办法更快一点? 有没有办法一次性收缩所有数据库? 代码如下:alter database 数据库名 set recovery simp
- 前言如果你和我一样偶尔看看股票,看看自己关注的股票是涨了还是跌了,或者想快速获取到想看的头条新闻,我们不必把过多的注意力放在去寻找上面,我们
- 什么是作用域程序的执行,离不开作用域,也必须在作用域中才能将代码正确的执行。所以作用域到底是什么,通俗的说,可以这样理解:作用域就是定义变量
- --创建 test 表 ,插入数据CREATE TABLE test(code varchar(50), [values] varchar(
- 本文实例讲述了Python机器学习之决策树算法。分享给大家供大家参考,具体如下:决策树学习是应用最广泛的归纳推理算法之一,是一种逼近离散值目
- 本文实例讲述了php实现的日历程序。分享给大家供大家参考。具体如下:<?php /* * php 输出日历程序 */ header(&
- 概述pandas中与库版本或依赖库版本相关的API主要有以下4个:pandas.__version__:查看pandas简要版本信息。pan
- 前言文章中的代码是参考基于Pytorch的特征图提取编写的代码本身很简单这里只做简单的描述。1. 效果图先看效果图(第一张是原图,后面的都是
- 1.过滤器的使用1.过滤器和测试器在Python中,如果需要对某个变量进行处理,我们可以通过函数来实现。在模板中,我们则是通过过滤器来实现的