Fixed identifier for a machine (uuid.getnode)

uuid.getnode will return the same value for every call wthinin a single run of your app. If it has to defer to the random algorithm, then you will get a different value when you start a new instance of your app.

The implementation for getNode shows why. This is sort of what the routine looks like in python 3.7 (comments mine, code simplified for clarity)

_node = None

def getnode():

    global _node
    if _node is not None:
        # Return cached value
        return _node

    # calculate node using platform specific logic like unix functions, ifconfig, etc 
    _node = _get_node_from_platform()
    if not _node:
        # couldn't get node id from the system. Just make something up
        _node = _get_random_node() 
    return _node

For Mac/iphone you can try below command:

import subprocess
subprocess.check_output("ioreg -rd1 -c IOPlatformExpertDevice | grep -E '(UUID)'", shell=True).split('"')[-2] # for me i got it on list value -2 if having any trouble try getting it with any alternative list element.

I managed to test the first part on my android device and on each new python run it created random number, so it's not usable at all for this purpose.

The second problem kind of drowned itself, because if in the docs it mentioned that it may return any one of them, then it's not something you could rely on (+I couldn't find a machine I could test it on). A nice package netifaces came to rescue, which does a similar thing

netifaces.interfaces() # returns e.g. ['lo', 'eth0', 'tun2']

netifaces.ifaddresses('eth0')[netifaces.AF_LINK]
# returns [{'addr': '08:00:27:50:f2:51', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]

However I rather gave up using MACs, I got something rather more stable.

Now to the identifiers:

1) Windows:

Executing this one and getting output may be good enough:

wmic csproduct get UUID

or the one I used and is available in registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography):

import _winreg
registry = _winreg.HKEY_LOCAL_MACHINE
address = 'SOFTWARE\\Microsoft\\Cryptography'
keyargs = _winreg.KEY_READ | _winreg.KEY_WOW64_64KEY
key = _winreg.OpenKey(registry, address, 0, keyargs)
value = _winreg.QueryValueEx(key, 'MachineGuid')
_winreg.CloseKey(key)
unique = value[0]

2) Linux:

/sys/class/dmi/id/board_serial

or

/sys/class/dmi/id/product_uuid

or if not root:

cat /var/lib/dbus/machine-id

3) Android:

If you are working with python and don't want to mess with Java stuff, then this should work pretty good:

import subprocess
cmd = ['getprop', 'ril.serialnumber']
self.unique = subprocess.check_output(cmd)[:-1]

but if you like Java, then go for this answer although even ANDROID_ID's uniqueness is rather debatable if it's allowed to change, therefore a serial number is most likely a safer bet.

Note that like it's already mentioned in the linked answer, even ril.serialnumber can be null/empty or non-existing (missing key). Same thing happens even with the official Android API where it's clearly stated this:

A hardware serial number, if available.

Mac/iPhone: I couldn't find any solution as I don't have access to any of these, but if there is a variable that holds the machine id value, then you should be able to get there with simple subprocess.check_output()