Python獲取當(dāng)前公網(wǎng)ip并自動(dòng)斷開寬帶連接實(shí)例代碼
今天寫了一個(gè)獲取當(dāng)前公網(wǎng)ip并且自動(dòng)斷開寬帶連接的文件,和大家分享下。
這個(gè)文件的具體用途大家懂的,可以盡管拿去用,不過目前只適用于Windows平臺(tái),我的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
基本的注釋都有,大家可以自己參考。
總結(jié)
以上就是本文關(guān)于Python獲取當(dāng)前公網(wǎng)ip并自動(dòng)斷開寬帶連接實(shí)例代碼的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Python 改變數(shù)組類型為uint8的實(shí)現(xiàn)
這篇文章主要介紹了Python 改變數(shù)組類型為uint8的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Pycharm學(xué)習(xí)教程(5) Python快捷鍵相關(guān)設(shè)置
這篇文章主要為大家詳細(xì)介紹了最全的Pycharm學(xué)習(xí)教程第五篇,Python快捷鍵相關(guān)設(shè)置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Python基礎(chǔ)之標(biāo)準(zhǔn)庫和常用的第三方庫案例教程
這篇文章主要介紹了Python基礎(chǔ)之標(biāo)準(zhǔn)庫和常用的第三方庫案例教程,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
Python實(shí)現(xiàn)的簡單模板引擎功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的簡單模板引擎功能,結(jié)合具體實(shí)例形式分析了Python模版引擎的定義與使用方法,需要的朋友可以參考下2017-09-09
Python 如何實(shí)時(shí)向文件寫入數(shù)據(jù)(附代碼)
這篇文章主要介紹了Python 如何實(shí)時(shí)向文件寫入數(shù)據(jù)(附代碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Python的Flask框架中@app.route的用法教程
這篇文章主要介紹了Python的Flask框架中@app.route的用法教程,包括相關(guān)的正則表達(dá)式講解,是Flask學(xué)習(xí)過程當(dāng)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-03-03

