如何使用Python提取MAC地址
MAC地址也称为物理地址,是分配给计算机NIC(网络接口卡)的唯一标识符。NIC有助于将计算机与网络中的其他计算机连接起来。MAC 地址与如今位于每台计算机内部的网络接口卡或网络接口控制器 (NIC) 相关联。NIC 充当传输介质,将数据转换为电信号,然后可以通过 Web 传输。

由六组两个字符或数字组成,冒号或连字符可以分隔。这个数字的限制是由于地址本身的长度为 48 位。一个典型的 MAC 地址有六组两个十六进制数字。例如,00:05:85:00:34:SG
MAC地址的用途:
- 在IP地址经常变化的地方很有用。帮助网络管理员。获取有关网络流量的信息。
- 帮助我们配置哪些计算机可以连接我们的计算机。通过这种方式可以过滤潜在的垃圾邮件/病毒攻击。
- 有助于从世界各地的其他计算机中唯一地识别计算机。
1、使用mac模块
要获取设备的物理地址,我们使用Python的getmac模块。

from getmac import get_mac_address as gma
print(gma())
输出:
'3c:7e:94:8f:d0:34'
2、使用uuid.getnode()
getnode()可用于提取计算机的MAC地址。该函数在uuid模块中定义。下面给出的图解代码显示了如何使用uuid1()函数为给定主机生成由其MAC地址标识的UUID。

import uuid
print(hex(uuid.getnode()))
输出:
0x163e990bdb
缺点:明显的缺点是输出不是格式化的形式。
推荐:如何在Windows 10中为Python安装dlib库
3、使用getnode()+format()

import uuid
print ("The MAC address in formatted way is : ", end="")
print (':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)for ele in range(0,8*6,8)][::-1]))
输出:
The MAC address in formatted way is : 9c:5c:8e:92:00:1e
缺点:这段代码看起来很复杂。
4、使用getnode()+findall()+re()[降低复杂度]

import re, uuid
print ("The MAC address in formatted and less complex way is : ", end="")
print (':'.join(re.findall('..', '%012x' % uuid.getnode())))
输出:
The MAC address in formatted and less complex way is : 9c:5c:8e:92:00:1e
总结
以上是晓得博客为你介绍的如何使用Python提取MAC地址的全部内容,希望对你的Python编程学习有帮助,如有问题可随时联系我们。