How to get Mac address of ethernet port?

Chris Angelico rosuav at gmail.com
Sat Jan 11 09:52:12 EST 2014


On Sun, Jan 12, 2014 at 1:35 AM, Andriy Kornatskyy
<andriy.kornatskyy at live.com> wrote:
> from uuid import getnode as get_mac
> '%012x' % get_mac()
>

Code golf! Put colons in that, with as little code as possible.

# Way too verbose.
import uuid
l=list("%012x"%uuid.getnode())
l[10:10]=l[8:8]=l[6:6]=l[4:4]=l[2:2]=':'
mac = ''.join(l)

# Shorter but not short enough
import uuid
s="%012x"%uuid.getnode()
mac = ':'.join(s[i*2:i*2+2] for i in range(6))

:)

ChrisA



More information about the Python-list mailing list