Back to index

python3.2  3.2.2
Classes | Functions | Variables
uuid Namespace Reference

Classes

class  UUID

Functions

def _find_mac
def _ifconfig_getnode
def _ipconfig_getnode
def _netbios_getnode
def _unixdll_getnode
def _windll_getnode
def _random_getnode
def getnode
def uuid1
def uuid3
def uuid4
def uuid5

Variables

string __author__ = 'Ka-Ping Yee <ping@zesty.ca>'
 int_ = int
 bytes_ = bytes
 _uuid_generate_random = _uuid_generate_time_UuidCreateNone
tuple lib = ctypes.CDLL(ctypes.util.find_library(libname))
 _uuid_generate_time = lib.uuid_generate_time
tuple _UuidCreate
 _node = None
 _last_timestamp = None
tuple NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
tuple NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8')
tuple NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
tuple NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')

Function Documentation

def uuid._find_mac (   command,
  args,
  hw_identifiers,
  get_index 
) [private]

Definition at line 314 of file uuid.py.

00314 
00315 def _find_mac(command, args, hw_identifiers, get_index):
00316     import os
00317     for dir in ['', '/sbin/', '/usr/sbin']:
00318         executable = os.path.join(dir, command)
00319         if not os.path.exists(executable):
00320             continue
00321 
00322         try:
00323             # LC_ALL to get English output, 2>/dev/null to
00324             # prevent output on stderr
00325             cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args)
00326             with os.popen(cmd) as pipe:
00327                 for line in pipe:
00328                     words = line.lower().split()
00329                     for i in range(len(words)):
00330                         if words[i] in hw_identifiers:
00331                             return int(
00332                                 words[get_index(i)].replace(':', ''), 16)
00333         except IOError:
00334             continue
00335     return None

Here is the call graph for this function:

Here is the caller graph for this function:

def uuid._ifconfig_getnode ( ) [private]
Get the hardware address on Unix by running ifconfig.

Definition at line 336 of file uuid.py.

00336 
00337 def _ifconfig_getnode():
00338     """Get the hardware address on Unix by running ifconfig."""
00339 
00340     # This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
00341     for args in ('', '-a', '-av'):
00342         mac = _find_mac('ifconfig', args, ['hwaddr', 'ether'], lambda i: i+1)
00343         if mac:
00344             return mac
00345 
00346     import socket
00347     ip_addr = socket.gethostbyname(socket.gethostname())
00348 
00349     # Try getting the MAC addr from arp based on our IP address (Solaris).
00350     mac = _find_mac('arp', '-an', [ip_addr], lambda i: -1)
00351     if mac:
00352         return mac
00353 
00354     # This might work on HP-UX.
00355     mac = _find_mac('lanscan', '-ai', ['lan0'], lambda i: 0)
00356     if mac:
00357         return mac
00358 
00359     return None

Here is the call graph for this function:

Here is the caller graph for this function:

def uuid._ipconfig_getnode ( ) [private]
Get the hardware address on Windows by running ipconfig.exe.

Definition at line 360 of file uuid.py.

00360 
00361 def _ipconfig_getnode():
00362     """Get the hardware address on Windows by running ipconfig.exe."""
00363     import os, re
00364     dirs = ['', r'c:\windows\system32', r'c:\winnt\system32']
00365     try:
00366         import ctypes
00367         buffer = ctypes.create_string_buffer(300)
00368         ctypes.windll.kernel32.GetSystemDirectoryA(buffer, 300)
00369         dirs.insert(0, buffer.value.decode('mbcs'))
00370     except:
00371         pass
00372     for dir in dirs:
00373         try:
00374             pipe = os.popen(os.path.join(dir, 'ipconfig') + ' /all')
00375         except IOError:
00376             continue
00377         else:
00378             for line in pipe:
00379                 value = line.split(':')[-1].strip().lower()
00380                 if re.match('([0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value):
00381                     return int(value.replace('-', ''), 16)
00382         finally:
00383             pipe.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def uuid._netbios_getnode ( ) [private]
Get the hardware address on Windows using NetBIOS calls.
See http://support.microsoft.com/kb/118623 for details.

Definition at line 384 of file uuid.py.

00384 
00385 def _netbios_getnode():
00386     """Get the hardware address on Windows using NetBIOS calls.
00387     See http://support.microsoft.com/kb/118623 for details."""
00388     import win32wnet, netbios
00389     ncb = netbios.NCB()
00390     ncb.Command = netbios.NCBENUM
00391     ncb.Buffer = adapters = netbios.LANA_ENUM()
00392     adapters._pack()
00393     if win32wnet.Netbios(ncb) != 0:
00394         return
00395     adapters._unpack()
00396     for i in range(adapters.length):
00397         ncb.Reset()
00398         ncb.Command = netbios.NCBRESET
00399         ncb.Lana_num = ord(adapters.lana[i])
00400         if win32wnet.Netbios(ncb) != 0:
00401             continue
00402         ncb.Reset()
00403         ncb.Command = netbios.NCBASTAT
00404         ncb.Lana_num = ord(adapters.lana[i])
00405         ncb.Callname = '*'.ljust(16)
00406         ncb.Buffer = status = netbios.ADAPTER_STATUS()
00407         if win32wnet.Netbios(ncb) != 0:
00408             continue
00409         status._unpack()
00410         bytes = map(ord, status.adapter_address)
00411         return ((bytes[0]<<40) + (bytes[1]<<32) + (bytes[2]<<24) +
00412                 (bytes[3]<<16) + (bytes[4]<<8) + bytes[5])
00413 
00414 # Thanks to Thomas Heller for ctypes and for his help with its use here.
00415 
00416 # If ctypes is available, use it to find system routines for UUID generation.
# XXX This makes the module non-thread-safe!

Here is the call graph for this function:

Here is the caller graph for this function:

def uuid._random_getnode ( ) [private]
Get a random node ID, with eighth bit set as suggested by RFC 4122.

Definition at line 475 of file uuid.py.

00475 
00476 def _random_getnode():
00477     """Get a random node ID, with eighth bit set as suggested by RFC 4122."""
00478     import random
00479     return random.randrange(0, 1<<48) | 0x010000000000

Here is the call graph for this function:

Here is the caller graph for this function:

def uuid._unixdll_getnode ( ) [private]
Get the hardware address on Unix using ctypes.

Definition at line 463 of file uuid.py.

00463 
00464 def _unixdll_getnode():
00465     """Get the hardware address on Unix using ctypes."""
00466     _buffer = ctypes.create_string_buffer(16)
00467     _uuid_generate_time(_buffer)
00468     return UUID(bytes=bytes_(_buffer.raw)).node

Here is the call graph for this function:

Here is the caller graph for this function:

def uuid._windll_getnode ( ) [private]
Get the hardware address on Windows using ctypes.

Definition at line 469 of file uuid.py.

00469 
00470 def _windll_getnode():
00471     """Get the hardware address on Windows using ctypes."""
00472     _buffer = ctypes.create_string_buffer(16)
00473     if _UuidCreate(_buffer) == 0:
00474         return UUID(bytes=bytes_(_buffer.raw)).node

Here is the call graph for this function:

Here is the caller graph for this function:

def uuid.getnode ( )
Get the hardware address as a 48-bit positive integer.

The first time this runs, it may launch a separate program, which could
be quite slow.  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.

Definition at line 482 of file uuid.py.

00482 
00483 def getnode():
00484     """Get the hardware address as a 48-bit positive integer.
00485 
00486     The first time this runs, it may launch a separate program, which could
00487     be quite slow.  If all attempts to obtain the hardware address fail, we
00488     choose a random 48-bit number with its eighth bit set to 1 as recommended
00489     in RFC 4122.
00490     """
00491 
00492     global _node
00493     if _node is not None:
00494         return _node
00495 
00496     import sys
00497     if sys.platform == 'win32':
00498         getters = [_windll_getnode, _netbios_getnode, _ipconfig_getnode]
00499     else:
00500         getters = [_unixdll_getnode, _ifconfig_getnode]
00501 
00502     for getter in getters + [_random_getnode]:
00503         try:
00504             _node = getter()
00505         except:
00506             continue
00507         if _node is not None:
00508             return _node

Here is the caller graph for this function:

def uuid.uuid1 (   node = None,
  clock_seq = None 
)
Generate a UUID from a host ID, sequence number, and the current time.
If 'node' is not given, getnode() is used to obtain the hardware
address.  If 'clock_seq' is given, it is used as the sequence number;
otherwise a random 14-bit sequence number is chosen.

Definition at line 511 of file uuid.py.

00511 
00512 def uuid1(node=None, clock_seq=None):
00513     """Generate a UUID from a host ID, sequence number, and the current time.
00514     If 'node' is not given, getnode() is used to obtain the hardware
00515     address.  If 'clock_seq' is given, it is used as the sequence number;
00516     otherwise a random 14-bit sequence number is chosen."""
00517 
00518     # When the system provides a version-1 UUID generator, use it (but don't
00519     # use UuidCreate here because its UUIDs don't conform to RFC 4122).
00520     if _uuid_generate_time and node is clock_seq is None:
00521         _buffer = ctypes.create_string_buffer(16)
00522         _uuid_generate_time(_buffer)
00523         return UUID(bytes=bytes_(_buffer.raw))
00524 
00525     global _last_timestamp
00526     import time
00527     nanoseconds = int(time.time() * 1e9)
00528     # 0x01b21dd213814000 is the number of 100-ns intervals between the
00529     # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
00530     timestamp = int(nanoseconds/100) + 0x01b21dd213814000
00531     if _last_timestamp is not None and timestamp <= _last_timestamp:
00532         timestamp = _last_timestamp + 1
00533     _last_timestamp = timestamp
00534     if clock_seq is None:
00535         import random
00536         clock_seq = random.randrange(1<<14) # instead of stable storage
00537     time_low = timestamp & 0xffffffff
00538     time_mid = (timestamp >> 32) & 0xffff
00539     time_hi_version = (timestamp >> 48) & 0x0fff
00540     clock_seq_low = clock_seq & 0xff
00541     clock_seq_hi_variant = (clock_seq >> 8) & 0x3f
00542     if node is None:
00543         node = getnode()
00544     return UUID(fields=(time_low, time_mid, time_hi_version,
00545                         clock_seq_hi_variant, clock_seq_low, node), version=1)

Here is the call graph for this function:

Here is the caller graph for this function:

def uuid.uuid3 (   namespace,
  name 
)
Generate a UUID from the MD5 hash of a namespace UUID and a name.

Definition at line 546 of file uuid.py.

00546 
00547 def uuid3(namespace, name):
00548     """Generate a UUID from the MD5 hash of a namespace UUID and a name."""
00549     from hashlib import md5
00550     hash = md5(namespace.bytes + bytes(name, "utf-8")).digest()
00551     return UUID(bytes=hash[:16], version=3)

Here is the caller graph for this function:

def uuid.uuid4 ( )
Generate a random UUID.

Definition at line 552 of file uuid.py.

00552 
00553 def uuid4():
00554     """Generate a random UUID."""
00555 
00556     # When the system provides a version-4 UUID generator, use it.
00557     if _uuid_generate_random:
00558         _buffer = ctypes.create_string_buffer(16)
00559         _uuid_generate_random(_buffer)
00560         return UUID(bytes=bytes_(_buffer.raw))
00561 
00562     # Otherwise, get randomness from urandom or the 'random' module.
00563     try:
00564         import os
00565         return UUID(bytes=os.urandom(16), version=4)
00566     except:
00567         import random
00568         bytes = bytes_(random.randrange(256) for i in range(16))
00569         return UUID(bytes=bytes, version=4)

Here is the call graph for this function:

Here is the caller graph for this function:

def uuid.uuid5 (   namespace,
  name 
)
Generate a UUID from the SHA-1 hash of a namespace UUID and a name.

Definition at line 570 of file uuid.py.

00570 
00571 def uuid5(namespace, name):
00572     """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
00573     from hashlib import sha1
00574     hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest()
00575     return UUID(bytes=hash[:16], version=5)
00576 
00577 # The following standard UUIDs are for use with uuid3() or uuid5().

Here is the caller graph for this function:


Variable Documentation

string uuid.__author__ = 'Ka-Ping Yee <ping@zesty.ca>'

Definition at line 47 of file uuid.py.

Definition at line 509 of file uuid.py.

uuid._node = None

Definition at line 480 of file uuid.py.

uuid._uuid_generate_random = _uuid_generate_time_UuidCreateNone

Definition at line 417 of file uuid.py.

uuid._uuid_generate_time = lib.uuid_generate_time

Definition at line 431 of file uuid.py.

Initial value:
00001 getattr(lib, 'UuidCreateSequential',
00002                           getattr(lib, 'UuidCreate', None))

Definition at line 458 of file uuid.py.

Definition at line 54 of file uuid.py.

Definition at line 53 of file uuid.py.

Definition at line 425 of file uuid.py.

tuple uuid.NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')

Definition at line 578 of file uuid.py.

tuple uuid.NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')

Definition at line 580 of file uuid.py.

tuple uuid.NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8')

Definition at line 579 of file uuid.py.

tuple uuid.NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')

Definition at line 581 of file uuid.py.