Getting mac address

I’m using the module ( re, uuid ) to capture the mac address and handle the result obtained. Running pyscript I get a different and changeable mac address when I reload the html page. Testing in pure python code I can get true mac address which is static and immutable.

  • Why is the mac address obtained is the fake and dynamic one using Pyscript?
  • How do I get the real mac address using Pyscript?

Below is the code example:

import re, uuid
print (':'.join(re.findall('..', '%012x' % uuid.getnode())))

I would love to be wrong, but I don’t think it’s possible in general to retrieve the MAC address of the machine that’s loading a particular page. I would think that’s a security issue.

(Apparently it’s possible in Internet Explorer using the ActiveX object, but that seems fairly niche. And using PyScript in IE is untested waters.)

As for why you’re getting a random number when you load the mac address in PyScript, that has to do with the behavior of uuid.getnode():

If all attempts to obtain the hardware address fail, we choose a random 48-bit number with the multicast bit (least significant bit of the first octet) set to 1 as recommended in RFC 4122.

Out of curiosity, what are you using the MAC address for?

Using purely python language, I can get my original mac address, I think ( uuid.getnode() ), has nothing to do with it! Already using ( pyscript ) the result I have is a different and dynamic mac address when the html page is loaded.

Regarding the reason for use, it’s for security reasons.

uuid.getnode() is how Python is retrieving your hardware mac address when you run your code locally. Your example code is using the re (regex) module to parse and format the 48-bit number it returns as a MAC address. When uuid.getnode() fails to find an answer, it picks a random number each time, hence the “dynamic” address you’re seeing. See the documentation on that function above.

PyScript runs purely inside your browser window (via Web Assembly and JavaScript), and therefore has certain limitations by definition. For example, programs running inside the browser cannot access your hard drive (except via spcific actions or with explicit permission), cannot call other programs on your harddisk, cannot close windows they have not opened… accessing the details of the network hardware the user is running is one of many things that programs in the browser cannot do. At least, at this moment in time.

Exactly, (certain limitations) prevent getting device mac address. Then generate a random address as ( uuid.getnode()), for not finding.

If you just want a random mac address then you can do this: virtualization - MAC address generator in python - Stack Overflow

Generating a random mac address for uuid.getnode() is a violation of its documented purpose: uuid — UUID objects according to RFC 4122 — Python 3.10.8 documentation