本文主要研究的是使用Python获取本机所有网卡ip,掩码和广播地址,分享了相关的实例代码,具体介绍如下。

搜了一天,竟然没找到一段合适的代码来获取机器中所有网卡的ip,掩码和广播地址,大部分都是用socket,但是socket通常返回的要不就是内网地址,要不就是公网地址,不能够找到所有地址,真的太忧桑了,决定自己通过ifconfig或ipconfig的返回信息,一步步地过滤了。这次的代码主要用到了正则表达式和subprocess模块,而且为了兼容所有平台(win,linux和mac),也用到了platform来判断系统类型,不说太多,代码如下:

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))

总结

以上就是本文关于Python获取本机所有网卡ip,掩码和广播地址实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

标签:
python,ip,掩码,python,获取网卡ip,python获取网卡ip地址,python,子网掩码

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?