python - 在python中获取MAC地址并附加到字符串

标签 python python-2.7

如何在 python 中以以下形式获取我的 mac 地址:\x##\x##\x##\x##\x##\x##?
我正在尝试构建以太网 header 并自动生成源硬件地址。

我正在使用 uuid.getnode() 获取 48 位整数形式的 mac 地址。我如何转换它以便我可以将它附加到已经采用该格式的字符串?
即:eth_header = "\xff\xff\xff\xff\xff\xff"+ magicconvert(uuid.getnode())

最佳答案

用户nneonneo 9月13日回答。归功于他:

    '-'.join('%02X' % ((uuid.getnode() >> 8*i) & 0xff) for i in reversed(xrange(6)))

编辑它以按照您喜欢的格式为您提供字符串。

我还想借此机会说“uuid.getnode()”很可能会返回一个随机值,或者它可能会获取您系统上任何随机网络接口(interface)的 MAC 地址。据我所知,真的没有办法知道。

关于python - 在python中获取MAC地址并附加到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13864608/

相关文章:

Python数组排序

python - 设置 matplotlib 3D 图的刻度颜色

Python打印多行?

regex - 从 URL 中删除文件名

python - 基于状态字段需要 bool 字段

python - 如何使用 SWIG 在 Python 中将 operator<< 包装到 __str__?

python - 欧拉计划 #11 : Python solution not working

python - 类型错误 : unsupported operand type(s) for ** or pow(): 'str' and 'int'

python - 像典型的过滤程序一样解析 cmd args

python - Django ModelForm的插入表单也显示已有记录