亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python獲取本機(jī)所有網(wǎng)卡ip,掩碼和廣播地址實(shí)例代碼

 更新時(shí)間:2018年01月22日 08:38:51   作者:davelam1991  
這篇文章主要介紹了Python獲取本機(jī)所有網(wǎng)卡ip,掩碼和廣播地址實(shí)例代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

本文主要研究的是使用Python獲取本機(jī)所有網(wǎng)卡ip,掩碼和廣播地址,分享了相關(guān)的實(shí)例代碼,具體介紹如下。

搜了一天,竟然沒(méi)找到一段合適的代碼來(lái)獲取機(jī)器中所有網(wǎng)卡的ip,掩碼和廣播地址,大部分都是用socket,但是socket通常返回的要不就是內(nèi)網(wǎng)地址,要不就是公網(wǎng)地址,不能夠找到所有地址,真的太憂桑了,決定自己通過(guò)ifconfig或ipconfig的返回信息,一步步地過(guò)濾了。這次的代碼主要用到了正則表達(dá)式和subprocess模塊,而且為了兼容所有平臺(tái)(win,linux和mac),也用到了platform來(lái)判斷系統(tǒng)類型,不說(shuō)太多,代碼如下:

import subprocess
import re
import platform


def find_all_ip(platform):
 ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}'
 if platform == "Darwin" or platform == "Linux":
  ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
  output = ipconfig_process.stdout.read()
  ip_pattern = re.compile('(inet %s)' % ipstr)
  if platform == "Linux":
   ip_pattern = re.compile('(inet addr:%s)' % ipstr)
  pattern = re.compile(ipstr)
  iplist = []
  for ipaddr in re.finditer(ip_pattern, str(output)):
   ip = pattern.search(ipaddr.group())
   if ip.group() != "127.0.0.1":
    iplist.append(ip.group())
  return iplist
 elif platform == "Windows":
  ipconfig_process = subprocess.Popen("ipconfig", stdout=subprocess.PIPE)
  output = ipconfig_process.stdout.read()
  ip_pattern = re.compile("IPv4 Address(\. )*: %s" % ipstr)
  pattern = re.compile(ipstr)
  iplist = []
  for ipaddr in re.finditer(ip_pattern, str(output)):
   ip = pattern.search(ipaddr.group())
   if ip.group() != "127.0.0.1":
    iplist.append(ip.group())
  return iplist


def find_all_mask(platform):
 ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}'
 maskstr = '0x([0-9a-f]{8})'
 if platform == "Darwin" or platform == "Linux":
  ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
  output = ipconfig_process.stdout.read()
  mask_pattern = re.compile('(netmask %s)' % maskstr)
  pattern = re.compile(maskstr)
  if platform == "Linux":
   mask_pattern = re.compile(r'Mask:%s' % ipstr)
   pattern = re.compile(ipstr)
  masklist = []
  for maskaddr in mask_pattern.finditer(str(output)):
   mask = pattern.search(maskaddr.group())
   if mask.group() != '0xff000000' and mask.group() != '255.0.0.0':
    masklist.append(mask.group())
  return masklist
 elif platform == "Windows":
  ipconfig_process = subprocess.Popen("ipconfig", stdout=subprocess.PIPE)
  output = ipconfig_process.stdout.read()
  mask_pattern = re.compile(r"Subnet Mask (\. )*: %s" % ipstr)
  pattern = re.compile(ipstr)
  masklist = []
  for maskaddr in mask_pattern.finditer(str(output)):
   mask = pattern.search(maskaddr.group())
   if mask.group() != '255.0.0.0':
    masklist.append(mask.group())
  return masklist


def get_broad_addr(ipstr, maskstr):
 iptokens = map(int, ipstr.split("."))
 masktokens = map(int, maskstr.split("."))
 broadlist = []
 for i in range(len(iptokens)):
  ip = iptokens[i]
  mask = masktokens[i]
  broad = ip & mask | (~mask & 255)
  broadlist.append(broad)
 return '.'.join(map(str, broadlist))


def find_all_broad(platform):
 ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}'
 if platform == "Darwin" or platform == "Linux":
  ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
  output = (ipconfig_process.stdout.read())
  broad_pattern = re.compile('(broadcast %s)' % ipstr)
  if platform == "Linux":
   broad_pattern = re.compile(r'Bcast:%s' % ipstr)
  pattern = re.compile(ipstr)
  broadlist = []
  for broadaddr in broad_pattern.finditer(str(output)):
   broad = pattern.search(broadaddr.group())
   broadlist.append(broad.group())
  return broadlist
 elif platform == "Windows":
  iplist = find_all_ip(platform)
  masklist = find_all_mask(platform)
  broadlist = []
  for i in range(len(iplist)):
   broadlist.append(get_broad_addr(iplist[i], masklist[i]))
  return broadlist


system = platform.system()
print(find_all_ip(system))
print(find_all_mask(system))
print(find_all_broad(system))

總結(jié)

以上就是本文關(guān)于Python獲取本機(jī)所有網(wǎng)卡ip,掩碼和廣播地址實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • Python實(shí)現(xiàn)判斷字符串中包含某個(gè)字符的判斷函數(shù)示例

    Python實(shí)現(xiàn)判斷字符串中包含某個(gè)字符的判斷函數(shù)示例

    這篇文章主要介紹了Python實(shí)現(xiàn)判斷字符串中包含某個(gè)字符的判斷函數(shù),涉及Python自定義函數(shù)中使用find方法針對(duì)字符串的簡(jiǎn)單判斷查找相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)

    Python解析json之ValueError: Expecting property name enclosed in

    這篇文章主要給大家介紹了關(guān)于Python解析json報(bào)錯(cuò):ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)的解決方法,文中介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。
    2017-07-07
  • 深入剖析Python的爬蟲(chóng)框架Scrapy的結(jié)構(gòu)與運(yùn)作流程

    深入剖析Python的爬蟲(chóng)框架Scrapy的結(jié)構(gòu)與運(yùn)作流程

    這篇文章主要介紹了Python的爬蟲(chóng)框架Scrapy的結(jié)構(gòu)與運(yùn)作流程,并以一個(gè)實(shí)際的項(xiàng)目來(lái)講解Scrapy的原理機(jī)制,十分推薦!需要的朋友可以參考下
    2016-01-01
  • python程序控制NAO機(jī)器人行走

    python程序控制NAO機(jī)器人行走

    這篇文章主要為大家詳細(xì)介紹了python程序控制NAO機(jī)器人行走,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Django博客系統(tǒng)注冊(cè)之創(chuàng)建用戶模塊應(yīng)用

    Django博客系統(tǒng)注冊(cè)之創(chuàng)建用戶模塊應(yīng)用

    本文主要介紹了Django博客系統(tǒng)注冊(cè)之創(chuàng)建用戶模塊應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • python計(jì)算機(jī)視覺(jué)opencv卡號(hào)識(shí)別示例詳解

    python計(jì)算機(jī)視覺(jué)opencv卡號(hào)識(shí)別示例詳解

    這篇文章主要為大家介紹了python計(jì)算機(jī)視覺(jué)opencv卡號(hào)識(shí)別的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下 希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • Python中typing模塊的具體使用

    Python中typing模塊的具體使用

    本文主要介紹了Python中typing模塊的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 詳解python中GPU版本的opencv常用方法介紹

    詳解python中GPU版本的opencv常用方法介紹

    這篇文章主要介紹了詳解python中GPU版本的opencv常用方法介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Django自帶用戶認(rèn)證系統(tǒng)使用方法解析

    Django自帶用戶認(rèn)證系統(tǒng)使用方法解析

    這篇文章主要介紹了Django自帶用戶認(rèn)證系統(tǒng)使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Python初學(xué)者必須掌握的25個(gè)內(nèi)置函數(shù)詳解

    Python初學(xué)者必須掌握的25個(gè)內(nèi)置函數(shù)詳解

    這篇文章主要介紹了Python25個(gè)常用內(nèi)置函數(shù)總結(jié),本文羅列了數(shù)學(xué)相關(guān) 、功能相關(guān)、類型轉(zhuǎn)換、字符串處理、序列處理函數(shù)等常用內(nèi)置函數(shù),需要的朋友可以參考下
    2021-09-09

最新評(píng)論