我们知道 MAC 地址是硬件地址,这意味着它对于安装在我们 PC 上的网卡是唯一的。它始终是唯一的,这意味着本地网络上的任何两个设备都不可能具有相同的 MAC 地址。
MAC 地址的主要目的是为局域网 (LAN) 或其他网络上的每个节点提供唯一的硬件地址或物理地址。节点是指计算机或其他设备(例如打印机或路由器)将保持连接到网络的点。
方法 1
使用 uuid.getnode()
在此示例中,getnode() 可用于提取计算机的 MAC 地址。此函数在 uuid 模块中定义。
示例代码
import uuid
print (hex(uuid.getnode()))
输出
0x242ac110002L
方法 2
使用 getnode() + format() [这是为了更好的格式化]
示例代码
import uuid
# 在每个 2 位数字后,连接 getnode() 的元素。
print ("格式化的 MAC 地址为 : ", end="")
print (':'.join(['{:02x}'.format((uuid.getnode() >> elements) & 0xff)
for elements in range(0,2*6,2)][::-1]))
输出
The formatted MAC address is : 3e:f8:e2:8b:2c:b3
方法 3
使用 getnode() + findall() + re()[这是为了降低复杂性]
示例代码
import re, uuid
# 在每个 2 位数字后,连接 getnode() 的元素。
# 使用正则表达式
print ("The MAC address in expressed in formatted and less complex way : ", end="")
print (':'.join(re.findall('..', '%012x' % uuid.getnode())))
输出
The MAC address in expressed in formatted and less complex way : 18:5e:0f:d4:f8:b3
相关文章
- Python 程序计算 n+nm+nmm.......+n(m 次)。
- Python 中的配置文件解析器 (configparser)
- 如何在 Python 中将时间秒转换为 h:m:s 格式?
- 内部 Python 对象序列化 (marshal)
- // 运算符的作用是什么?
- 如何从 Python 字典中删除键?
- 如何将 Python DateTime 字符串转换为整数毫秒?
- 用于自动刷新 Excel 电子表格的 Python 脚本
- 使用 Python openpyxl 模块读取和写入 excel 文件
- Python 字节码反汇编器
有用资源
- Python 参考教程 - 该教程包含有关 Python 的更多信息:https://www.w3schools.cn/python/