推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
linuxzpf
V2EX  ›  Python

python 如何获取 Mac 地址?

  •  
  •   linuxzpf · Jan 17, 2015 · 7148 views
    This topic created in 4274 days ago, the information mentioned may be changed or developed.

    我用uuid模块取Mac地址的,刚开始很完美,可是后来发现同一台主机取来的Mac地址经常变化,很不靠谱啊,有时候取到的根本不是本机的Mac,也不知道为什么会这样。
    求助怎么用Python取一台主机的Mac并且不会自己乱变,另外我的代码贴上来:
    def getMac():
    mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
    return ":".join([mac[e:e+2] for e in range(0,11,2)])

    6 replies  •  2019-06-11 09:21:43 +08:00
    fatbean
        1
    fatbean  
       Jan 17, 2015 via iPad
    你装了虚拟机就这样了
    linuxzpf
        2
    linuxzpf  
    OP
       Jan 17, 2015
    @fatbean 恰恰相反,我的vbox虚拟机取到的Mac都不会变,反而是取到本机的Mac变个不停,好烦
    s51431980
        3
    s51431980  
       Jan 17, 2015
    If you look at the getnode documentation it says that it will return a random long if it fails to detect the mac: "If all attempts to obtain the hardware address fail, we choose a random 48-bit number with its eighth bit set to 1 as recommended in RFC 4122."
    linuxzpf
        5
    linuxzpf  
    OP
       Jan 18, 2015
    @s51431980 这些都是不行的哦亲,完美符合我说的变来变去的情况嘛。
    trueGate
        6
    trueGate  
       Jun 11, 2019
    @s51431980 感谢提醒,最近遇到几台电脑出现了文档中问题,导致返回了 48bit 随机数。请问您是否研究过在 windows 平台下修复该问题的办法呢?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2441 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:05 · PVG 17:05 · LAX 02:05 · JFK 05:05
    ♥ Do have faith in what you're doing.