最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python獲取當(dāng)前公網(wǎng)ip并自動(dòng)斷開(kāi)寬帶連接實(shí)例代碼

 更新時(shí)間:2018年01月12日 15:33:58   作者:mingz2013  
這篇文章主要介紹了Python獲取當(dāng)前公網(wǎng)ip并自動(dòng)斷開(kāi)寬帶連接實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下

今天寫(xiě)了一個(gè)獲取當(dāng)前公網(wǎng)ip并且自動(dòng)斷開(kāi)寬帶連接的文件,和大家分享下。

這個(gè)文件的具體用途大家懂的,可以盡管拿去用,不過(guò)目前只適用于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)斷開(kāi)寬帶連接實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • Python 改變數(shù)組類型為uint8的實(shí)現(xiàn)

    Python 改變數(shù)組類型為uint8的實(shí)現(xiàn)

    這篇文章主要介紹了Python 改變數(shù)組類型為uint8的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python中打印詳細(xì)堆棧信息的技巧分享

    Python中打印詳細(xì)堆棧信息的技巧分享

    在 Python 開(kāi)發(fā)過(guò)程中,調(diào)試是一個(gè)不可或缺的環(huán)節(jié),當(dāng)代碼出現(xiàn)問(wèn)題時(shí),能夠快速準(zhǔn)確地定位問(wèn)題所在是提高開(kāi)發(fā)效率的關(guān)鍵,堆棧信息作為程序執(zhí)行過(guò)程中的調(diào)用記錄,對(duì)于理解程序的運(yùn)行狀態(tài)和定位錯(cuò)誤至關(guān)重要,需要的朋友可以參考下
    2024-11-11
  • Pycharm學(xué)習(xí)教程(5) Python快捷鍵相關(guān)設(shè)置

    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)庫(kù)和常用的第三方庫(kù)案例教程

    Python基礎(chǔ)之標(biāo)準(zhǔn)庫(kù)和常用的第三方庫(kù)案例教程

    這篇文章主要介紹了Python基礎(chǔ)之標(biāo)準(zhǔn)庫(kù)和常用的第三方庫(kù)案例教程,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Python實(shí)現(xiàn)的簡(jiǎn)單模板引擎功能示例

    Python實(shí)現(xiàn)的簡(jiǎn)單模板引擎功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的簡(jiǎn)單模板引擎功能,結(jié)合具體實(shí)例形式分析了Python模版引擎的定義與使用方法,需要的朋友可以參考下
    2017-09-09
  • mac在matplotlib中顯示中文的操作方法

    mac在matplotlib中顯示中文的操作方法

    這篇文章主要介紹了mac如何在matplotlib中顯示中文,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 在python中解決死鎖的問(wèn)題

    在python中解決死鎖的問(wèn)題

    這篇文章主要介紹了在python中解決死鎖的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • Python 如何實(shí)時(shí)向文件寫(xiě)入數(shù)據(jù)(附代碼)

    Python 如何實(shí)時(shí)向文件寫(xiě)入數(shù)據(jù)(附代碼)

    這篇文章主要介紹了Python 如何實(shí)時(shí)向文件寫(xiě)入數(shù)據(jù)(附代碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python的Flask框架中@app.route的用法教程

    Python的Flask框架中@app.route的用法教程

    這篇文章主要介紹了Python的Flask框架中@app.route的用法教程,包括相關(guān)的正則表達(dá)式講解,是Flask學(xué)習(xí)過(guò)程當(dāng)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-03-03
  • python實(shí)現(xiàn)圖像降噪

    python實(shí)現(xiàn)圖像降噪

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖像降噪,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評(píng)論

五指山市| 安乡县| 兰考县| 色达县| 乌拉特中旗| 朝阳区| 子洲县| 贵阳市| 阿荣旗| 河西区| 安丘市| 台中市| 泰宁县| 邮箱| 肥城市| SHOW| 大荔县| 博白县| 石嘴山市| 梁山县| 呼伦贝尔市| 威远县| 克拉玛依市| 罗源县| 体育| 侯马市| 宝应县| 潜江市| 奉节县| 阜平县| 谢通门县| 九江县| 偃师市| 朝阳县| 壶关县| 清河县| 山西省| 盱眙县| 射洪县| 壶关县| 南乐县|