Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: uuid.py
r"""UUID objects (universally unique identifiers) according to RFC 4122.
[0] Fix | Delete
[1] Fix | Delete
This module provides immutable UUID objects (class UUID) and the functions
[2] Fix | Delete
uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5
[3] Fix | Delete
UUIDs as specified in RFC 4122.
[4] Fix | Delete
[5] Fix | Delete
If all you want is a unique ID, you should probably call uuid1() or uuid4().
[6] Fix | Delete
Note that uuid1() may compromise privacy since it creates a UUID containing
[7] Fix | Delete
the computer's network address. uuid4() creates a random UUID.
[8] Fix | Delete
[9] Fix | Delete
Typical usage:
[10] Fix | Delete
[11] Fix | Delete
>>> import uuid
[12] Fix | Delete
[13] Fix | Delete
# make a UUID based on the host ID and current time
[14] Fix | Delete
>>> uuid.uuid1() # doctest: +SKIP
[15] Fix | Delete
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
[16] Fix | Delete
[17] Fix | Delete
# make a UUID using an MD5 hash of a namespace UUID and a name
[18] Fix | Delete
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
[19] Fix | Delete
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
[20] Fix | Delete
[21] Fix | Delete
# make a random UUID
[22] Fix | Delete
>>> uuid.uuid4() # doctest: +SKIP
[23] Fix | Delete
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
[24] Fix | Delete
[25] Fix | Delete
# make a UUID using a SHA-1 hash of a namespace UUID and a name
[26] Fix | Delete
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
[27] Fix | Delete
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
[28] Fix | Delete
[29] Fix | Delete
# make a UUID from a string of hex digits (braces and hyphens ignored)
[30] Fix | Delete
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
[31] Fix | Delete
[32] Fix | Delete
# convert a UUID to a string of hex digits in standard form
[33] Fix | Delete
>>> str(x)
[34] Fix | Delete
'00010203-0405-0607-0809-0a0b0c0d0e0f'
[35] Fix | Delete
[36] Fix | Delete
# get the raw 16 bytes of the UUID
[37] Fix | Delete
>>> x.bytes
[38] Fix | Delete
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
[39] Fix | Delete
[40] Fix | Delete
# make a UUID from a 16-byte string
[41] Fix | Delete
>>> uuid.UUID(bytes=x.bytes)
[42] Fix | Delete
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
[43] Fix | Delete
"""
[44] Fix | Delete
[45] Fix | Delete
import os
[46] Fix | Delete
import sys
[47] Fix | Delete
[48] Fix | Delete
from enum import Enum
[49] Fix | Delete
[50] Fix | Delete
[51] Fix | Delete
__author__ = 'Ka-Ping Yee <ping@zesty.ca>'
[52] Fix | Delete
[53] Fix | Delete
# The recognized platforms - known behaviors
[54] Fix | Delete
if sys.platform in ('win32', 'darwin'):
[55] Fix | Delete
_AIX = _LINUX = False
[56] Fix | Delete
else:
[57] Fix | Delete
import platform
[58] Fix | Delete
_platform_system = platform.system()
[59] Fix | Delete
_AIX = _platform_system == 'AIX'
[60] Fix | Delete
_LINUX = _platform_system == 'Linux'
[61] Fix | Delete
[62] Fix | Delete
RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
[63] Fix | Delete
'reserved for NCS compatibility', 'specified in RFC 4122',
[64] Fix | Delete
'reserved for Microsoft compatibility', 'reserved for future definition']
[65] Fix | Delete
[66] Fix | Delete
int_ = int # The built-in int type
[67] Fix | Delete
bytes_ = bytes # The built-in bytes type
[68] Fix | Delete
[69] Fix | Delete
[70] Fix | Delete
class SafeUUID(Enum):
[71] Fix | Delete
safe = 0
[72] Fix | Delete
unsafe = -1
[73] Fix | Delete
unknown = None
[74] Fix | Delete
[75] Fix | Delete
[76] Fix | Delete
class UUID:
[77] Fix | Delete
"""Instances of the UUID class represent UUIDs as specified in RFC 4122.
[78] Fix | Delete
UUID objects are immutable, hashable, and usable as dictionary keys.
[79] Fix | Delete
Converting a UUID to a string with str() yields something in the form
[80] Fix | Delete
'12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts
[81] Fix | Delete
five possible forms: a similar string of hexadecimal digits, or a tuple
[82] Fix | Delete
of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and
[83] Fix | Delete
48-bit values respectively) as an argument named 'fields', or a string
[84] Fix | Delete
of 16 bytes (with all the integer fields in big-endian order) as an
[85] Fix | Delete
argument named 'bytes', or a string of 16 bytes (with the first three
[86] Fix | Delete
fields in little-endian order) as an argument named 'bytes_le', or a
[87] Fix | Delete
single 128-bit integer as an argument named 'int'.
[88] Fix | Delete
[89] Fix | Delete
UUIDs have these read-only attributes:
[90] Fix | Delete
[91] Fix | Delete
bytes the UUID as a 16-byte string (containing the six
[92] Fix | Delete
integer fields in big-endian byte order)
[93] Fix | Delete
[94] Fix | Delete
bytes_le the UUID as a 16-byte string (with time_low, time_mid,
[95] Fix | Delete
and time_hi_version in little-endian byte order)
[96] Fix | Delete
[97] Fix | Delete
fields a tuple of the six integer fields of the UUID,
[98] Fix | Delete
which are also available as six individual attributes
[99] Fix | Delete
and two derived attributes:
[100] Fix | Delete
[101] Fix | Delete
time_low the first 32 bits of the UUID
[102] Fix | Delete
time_mid the next 16 bits of the UUID
[103] Fix | Delete
time_hi_version the next 16 bits of the UUID
[104] Fix | Delete
clock_seq_hi_variant the next 8 bits of the UUID
[105] Fix | Delete
clock_seq_low the next 8 bits of the UUID
[106] Fix | Delete
node the last 48 bits of the UUID
[107] Fix | Delete
[108] Fix | Delete
time the 60-bit timestamp
[109] Fix | Delete
clock_seq the 14-bit sequence number
[110] Fix | Delete
[111] Fix | Delete
hex the UUID as a 32-character hexadecimal string
[112] Fix | Delete
[113] Fix | Delete
int the UUID as a 128-bit integer
[114] Fix | Delete
[115] Fix | Delete
urn the UUID as a URN as specified in RFC 4122
[116] Fix | Delete
[117] Fix | Delete
variant the UUID variant (one of the constants RESERVED_NCS,
[118] Fix | Delete
RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)
[119] Fix | Delete
[120] Fix | Delete
version the UUID version number (1 through 5, meaningful only
[121] Fix | Delete
when the variant is RFC_4122)
[122] Fix | Delete
[123] Fix | Delete
is_safe An enum indicating whether the UUID has been generated in
[124] Fix | Delete
a way that is safe for multiprocessing applications, via
[125] Fix | Delete
uuid_generate_time_safe(3).
[126] Fix | Delete
"""
[127] Fix | Delete
[128] Fix | Delete
__slots__ = ('int', 'is_safe', '__weakref__')
[129] Fix | Delete
[130] Fix | Delete
def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
[131] Fix | Delete
int=None, version=None,
[132] Fix | Delete
*, is_safe=SafeUUID.unknown):
[133] Fix | Delete
r"""Create a UUID from either a string of 32 hexadecimal digits,
[134] Fix | Delete
a string of 16 bytes as the 'bytes' argument, a string of 16 bytes
[135] Fix | Delete
in little-endian order as the 'bytes_le' argument, a tuple of six
[136] Fix | Delete
integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version,
[137] Fix | Delete
8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
[138] Fix | Delete
the 'fields' argument, or a single 128-bit integer as the 'int'
[139] Fix | Delete
argument. When a string of hex digits is given, curly braces,
[140] Fix | Delete
hyphens, and a URN prefix are all optional. For example, these
[141] Fix | Delete
expressions all yield the same UUID:
[142] Fix | Delete
[143] Fix | Delete
UUID('{12345678-1234-5678-1234-567812345678}')
[144] Fix | Delete
UUID('12345678123456781234567812345678')
[145] Fix | Delete
UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
[146] Fix | Delete
UUID(bytes='\x12\x34\x56\x78'*4)
[147] Fix | Delete
UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
[148] Fix | Delete
'\x12\x34\x56\x78\x12\x34\x56\x78')
[149] Fix | Delete
UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
[150] Fix | Delete
UUID(int=0x12345678123456781234567812345678)
[151] Fix | Delete
[152] Fix | Delete
Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must
[153] Fix | Delete
be given. The 'version' argument is optional; if given, the resulting
[154] Fix | Delete
UUID will have its variant and version set according to RFC 4122,
[155] Fix | Delete
overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.
[156] Fix | Delete
[157] Fix | Delete
is_safe is an enum exposed as an attribute on the instance. It
[158] Fix | Delete
indicates whether the UUID has been generated in a way that is safe
[159] Fix | Delete
for multiprocessing applications, via uuid_generate_time_safe(3).
[160] Fix | Delete
"""
[161] Fix | Delete
[162] Fix | Delete
if [hex, bytes, bytes_le, fields, int].count(None) != 4:
[163] Fix | Delete
raise TypeError('one of the hex, bytes, bytes_le, fields, '
[164] Fix | Delete
'or int arguments must be given')
[165] Fix | Delete
if hex is not None:
[166] Fix | Delete
hex = hex.replace('urn:', '').replace('uuid:', '')
[167] Fix | Delete
hex = hex.strip('{}').replace('-', '')
[168] Fix | Delete
if len(hex) != 32:
[169] Fix | Delete
raise ValueError('badly formed hexadecimal UUID string')
[170] Fix | Delete
int = int_(hex, 16)
[171] Fix | Delete
if bytes_le is not None:
[172] Fix | Delete
if len(bytes_le) != 16:
[173] Fix | Delete
raise ValueError('bytes_le is not a 16-char string')
[174] Fix | Delete
bytes = (bytes_le[4-1::-1] + bytes_le[6-1:4-1:-1] +
[175] Fix | Delete
bytes_le[8-1:6-1:-1] + bytes_le[8:])
[176] Fix | Delete
if bytes is not None:
[177] Fix | Delete
if len(bytes) != 16:
[178] Fix | Delete
raise ValueError('bytes is not a 16-char string')
[179] Fix | Delete
assert isinstance(bytes, bytes_), repr(bytes)
[180] Fix | Delete
int = int_.from_bytes(bytes, byteorder='big')
[181] Fix | Delete
if fields is not None:
[182] Fix | Delete
if len(fields) != 6:
[183] Fix | Delete
raise ValueError('fields is not a 6-tuple')
[184] Fix | Delete
(time_low, time_mid, time_hi_version,
[185] Fix | Delete
clock_seq_hi_variant, clock_seq_low, node) = fields
[186] Fix | Delete
if not 0 <= time_low < 1<<32:
[187] Fix | Delete
raise ValueError('field 1 out of range (need a 32-bit value)')
[188] Fix | Delete
if not 0 <= time_mid < 1<<16:
[189] Fix | Delete
raise ValueError('field 2 out of range (need a 16-bit value)')
[190] Fix | Delete
if not 0 <= time_hi_version < 1<<16:
[191] Fix | Delete
raise ValueError('field 3 out of range (need a 16-bit value)')
[192] Fix | Delete
if not 0 <= clock_seq_hi_variant < 1<<8:
[193] Fix | Delete
raise ValueError('field 4 out of range (need an 8-bit value)')
[194] Fix | Delete
if not 0 <= clock_seq_low < 1<<8:
[195] Fix | Delete
raise ValueError('field 5 out of range (need an 8-bit value)')
[196] Fix | Delete
if not 0 <= node < 1<<48:
[197] Fix | Delete
raise ValueError('field 6 out of range (need a 48-bit value)')
[198] Fix | Delete
clock_seq = (clock_seq_hi_variant << 8) | clock_seq_low
[199] Fix | Delete
int = ((time_low << 96) | (time_mid << 80) |
[200] Fix | Delete
(time_hi_version << 64) | (clock_seq << 48) | node)
[201] Fix | Delete
if int is not None:
[202] Fix | Delete
if not 0 <= int < 1<<128:
[203] Fix | Delete
raise ValueError('int is out of range (need a 128-bit value)')
[204] Fix | Delete
if version is not None:
[205] Fix | Delete
if not 1 <= version <= 5:
[206] Fix | Delete
raise ValueError('illegal version number')
[207] Fix | Delete
# Set the variant to RFC 4122.
[208] Fix | Delete
int &= ~(0xc000 << 48)
[209] Fix | Delete
int |= 0x8000 << 48
[210] Fix | Delete
# Set the version number.
[211] Fix | Delete
int &= ~(0xf000 << 64)
[212] Fix | Delete
int |= version << 76
[213] Fix | Delete
object.__setattr__(self, 'int', int)
[214] Fix | Delete
object.__setattr__(self, 'is_safe', is_safe)
[215] Fix | Delete
[216] Fix | Delete
def __getstate__(self):
[217] Fix | Delete
d = {'int': self.int}
[218] Fix | Delete
if self.is_safe != SafeUUID.unknown:
[219] Fix | Delete
# is_safe is a SafeUUID instance. Return just its value, so that
[220] Fix | Delete
# it can be un-pickled in older Python versions without SafeUUID.
[221] Fix | Delete
d['is_safe'] = self.is_safe.value
[222] Fix | Delete
return d
[223] Fix | Delete
[224] Fix | Delete
def __setstate__(self, state):
[225] Fix | Delete
object.__setattr__(self, 'int', state['int'])
[226] Fix | Delete
# is_safe was added in 3.7; it is also omitted when it is "unknown"
[227] Fix | Delete
object.__setattr__(self, 'is_safe',
[228] Fix | Delete
SafeUUID(state['is_safe'])
[229] Fix | Delete
if 'is_safe' in state else SafeUUID.unknown)
[230] Fix | Delete
[231] Fix | Delete
def __eq__(self, other):
[232] Fix | Delete
if isinstance(other, UUID):
[233] Fix | Delete
return self.int == other.int
[234] Fix | Delete
return NotImplemented
[235] Fix | Delete
[236] Fix | Delete
# Q. What's the value of being able to sort UUIDs?
[237] Fix | Delete
# A. Use them as keys in a B-Tree or similar mapping.
[238] Fix | Delete
[239] Fix | Delete
def __lt__(self, other):
[240] Fix | Delete
if isinstance(other, UUID):
[241] Fix | Delete
return self.int < other.int
[242] Fix | Delete
return NotImplemented
[243] Fix | Delete
[244] Fix | Delete
def __gt__(self, other):
[245] Fix | Delete
if isinstance(other, UUID):
[246] Fix | Delete
return self.int > other.int
[247] Fix | Delete
return NotImplemented
[248] Fix | Delete
[249] Fix | Delete
def __le__(self, other):
[250] Fix | Delete
if isinstance(other, UUID):
[251] Fix | Delete
return self.int <= other.int
[252] Fix | Delete
return NotImplemented
[253] Fix | Delete
[254] Fix | Delete
def __ge__(self, other):
[255] Fix | Delete
if isinstance(other, UUID):
[256] Fix | Delete
return self.int >= other.int
[257] Fix | Delete
return NotImplemented
[258] Fix | Delete
[259] Fix | Delete
def __hash__(self):
[260] Fix | Delete
return hash(self.int)
[261] Fix | Delete
[262] Fix | Delete
def __int__(self):
[263] Fix | Delete
return self.int
[264] Fix | Delete
[265] Fix | Delete
def __repr__(self):
[266] Fix | Delete
return '%s(%r)' % (self.__class__.__name__, str(self))
[267] Fix | Delete
[268] Fix | Delete
def __setattr__(self, name, value):
[269] Fix | Delete
raise TypeError('UUID objects are immutable')
[270] Fix | Delete
[271] Fix | Delete
def __str__(self):
[272] Fix | Delete
hex = '%032x' % self.int
[273] Fix | Delete
return '%s-%s-%s-%s-%s' % (
[274] Fix | Delete
hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
[275] Fix | Delete
[276] Fix | Delete
@property
[277] Fix | Delete
def bytes(self):
[278] Fix | Delete
return self.int.to_bytes(16, 'big')
[279] Fix | Delete
[280] Fix | Delete
@property
[281] Fix | Delete
def bytes_le(self):
[282] Fix | Delete
bytes = self.bytes
[283] Fix | Delete
return (bytes[4-1::-1] + bytes[6-1:4-1:-1] + bytes[8-1:6-1:-1] +
[284] Fix | Delete
bytes[8:])
[285] Fix | Delete
[286] Fix | Delete
@property
[287] Fix | Delete
def fields(self):
[288] Fix | Delete
return (self.time_low, self.time_mid, self.time_hi_version,
[289] Fix | Delete
self.clock_seq_hi_variant, self.clock_seq_low, self.node)
[290] Fix | Delete
[291] Fix | Delete
@property
[292] Fix | Delete
def time_low(self):
[293] Fix | Delete
return self.int >> 96
[294] Fix | Delete
[295] Fix | Delete
@property
[296] Fix | Delete
def time_mid(self):
[297] Fix | Delete
return (self.int >> 80) & 0xffff
[298] Fix | Delete
[299] Fix | Delete
@property
[300] Fix | Delete
def time_hi_version(self):
[301] Fix | Delete
return (self.int >> 64) & 0xffff
[302] Fix | Delete
[303] Fix | Delete
@property
[304] Fix | Delete
def clock_seq_hi_variant(self):
[305] Fix | Delete
return (self.int >> 56) & 0xff
[306] Fix | Delete
[307] Fix | Delete
@property
[308] Fix | Delete
def clock_seq_low(self):
[309] Fix | Delete
return (self.int >> 48) & 0xff
[310] Fix | Delete
[311] Fix | Delete
@property
[312] Fix | Delete
def time(self):
[313] Fix | Delete
return (((self.time_hi_version & 0x0fff) << 48) |
[314] Fix | Delete
(self.time_mid << 32) | self.time_low)
[315] Fix | Delete
[316] Fix | Delete
@property
[317] Fix | Delete
def clock_seq(self):
[318] Fix | Delete
return (((self.clock_seq_hi_variant & 0x3f) << 8) |
[319] Fix | Delete
self.clock_seq_low)
[320] Fix | Delete
[321] Fix | Delete
@property
[322] Fix | Delete
def node(self):
[323] Fix | Delete
return self.int & 0xffffffffffff
[324] Fix | Delete
[325] Fix | Delete
@property
[326] Fix | Delete
def hex(self):
[327] Fix | Delete
return '%032x' % self.int
[328] Fix | Delete
[329] Fix | Delete
@property
[330] Fix | Delete
def urn(self):
[331] Fix | Delete
return 'urn:uuid:' + str(self)
[332] Fix | Delete
[333] Fix | Delete
@property
[334] Fix | Delete
def variant(self):
[335] Fix | Delete
if not self.int & (0x8000 << 48):
[336] Fix | Delete
return RESERVED_NCS
[337] Fix | Delete
elif not self.int & (0x4000 << 48):
[338] Fix | Delete
return RFC_4122
[339] Fix | Delete
elif not self.int & (0x2000 << 48):
[340] Fix | Delete
return RESERVED_MICROSOFT
[341] Fix | Delete
else:
[342] Fix | Delete
return RESERVED_FUTURE
[343] Fix | Delete
[344] Fix | Delete
@property
[345] Fix | Delete
def version(self):
[346] Fix | Delete
# The version bits are only meaningful for RFC 4122 UUIDs.
[347] Fix | Delete
if self.variant == RFC_4122:
[348] Fix | Delete
return int((self.int >> 76) & 0xf)
[349] Fix | Delete
[350] Fix | Delete
def _popen(command, *args):
[351] Fix | Delete
import os, shutil, subprocess
[352] Fix | Delete
executable = shutil.which(command)
[353] Fix | Delete
if executable is None:
[354] Fix | Delete
path = os.pathsep.join(('/sbin', '/usr/sbin'))
[355] Fix | Delete
executable = shutil.which(command, path=path)
[356] Fix | Delete
if executable is None:
[357] Fix | Delete
return None
[358] Fix | Delete
# LC_ALL=C to ensure English output, stderr=DEVNULL to prevent output
[359] Fix | Delete
# on stderr (Note: we don't have an example where the words we search
[360] Fix | Delete
# for are actually localized, but in theory some system could do so.)
[361] Fix | Delete
env = dict(os.environ)
[362] Fix | Delete
env['LC_ALL'] = 'C'
[363] Fix | Delete
proc = subprocess.Popen((executable,) + args,
[364] Fix | Delete
stdout=subprocess.PIPE,
[365] Fix | Delete
stderr=subprocess.DEVNULL,
[366] Fix | Delete
env=env)
[367] Fix | Delete
return proc
[368] Fix | Delete
[369] Fix | Delete
# For MAC (a.k.a. IEEE 802, or EUI-48) addresses, the second least significant
[370] Fix | Delete
# bit of the first octet signifies whether the MAC address is universally (0)
[371] Fix | Delete
# or locally (1) administered. Network cards from hardware manufacturers will
[372] Fix | Delete
# always be universally administered to guarantee global uniqueness of the MAC
[373] Fix | Delete
# address, but any particular machine may have other interfaces which are
[374] Fix | Delete
# locally administered. An example of the latter is the bridge interface to
[375] Fix | Delete
# the Touch Bar on MacBook Pros.
[376] Fix | Delete
#
[377] Fix | Delete
# This bit works out to be the 42nd bit counting from 1 being the least
[378] Fix | Delete
# significant, or 1<<41. We'll prefer universally administered MAC addresses
[379] Fix | Delete
# over locally administered ones since the former are globally unique, but
[380] Fix | Delete
# we'll return the first of the latter found if that's all the machine has.
[381] Fix | Delete
#
[382] Fix | Delete
# See https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local
[383] Fix | Delete
[384] Fix | Delete
def _is_universal(mac):
[385] Fix | Delete
return not (mac & (1 << 41))
[386] Fix | Delete
[387] Fix | Delete
def _find_mac(command, args, hw_identifiers, get_index):
[388] Fix | Delete
first_local_mac = None
[389] Fix | Delete
try:
[390] Fix | Delete
proc = _popen(command, *args.split())
[391] Fix | Delete
if not proc:
[392] Fix | Delete
return None
[393] Fix | Delete
with proc:
[394] Fix | Delete
for line in proc.stdout:
[395] Fix | Delete
words = line.lower().rstrip().split()
[396] Fix | Delete
for i in range(len(words)):
[397] Fix | Delete
if words[i] in hw_identifiers:
[398] Fix | Delete
try:
[399] Fix | Delete
word = words[get_index(i)]
[400] Fix | Delete
mac = int(word.replace(b':', b''), 16)
[401] Fix | Delete
if _is_universal(mac):
[402] Fix | Delete
return mac
[403] Fix | Delete
first_local_mac = first_local_mac or mac
[404] Fix | Delete
except (ValueError, IndexError):
[405] Fix | Delete
# Virtual interfaces, such as those provided by
[406] Fix | Delete
# VPNs, do not have a colon-delimited MAC address
[407] Fix | Delete
# as expected, but a 16-byte HWAddr separated by
[408] Fix | Delete
# dashes. These should be ignored in favor of a
[409] Fix | Delete
# real MAC address
[410] Fix | Delete
pass
[411] Fix | Delete
except OSError:
[412] Fix | Delete
pass
[413] Fix | Delete
return first_local_mac or None
[414] Fix | Delete
[415] Fix | Delete
def _ifconfig_getnode():
[416] Fix | Delete
"""Get the hardware address on Unix by running ifconfig."""
[417] Fix | Delete
# This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
[418] Fix | Delete
keywords = (b'hwaddr', b'ether', b'address:', b'lladdr')
[419] Fix | Delete
for args in ('', '-a', '-av'):
[420] Fix | Delete
mac = _find_mac('ifconfig', args, keywords, lambda i: i+1)
[421] Fix | Delete
if mac:
[422] Fix | Delete
return mac
[423] Fix | Delete
return None
[424] Fix | Delete
[425] Fix | Delete
def _ip_getnode():
[426] Fix | Delete
"""Get the hardware address on Unix by running ip."""
[427] Fix | Delete
# This works on Linux with iproute2.
[428] Fix | Delete
mac = _find_mac('ip', 'link', [b'link/ether'], lambda i: i+1)
[429] Fix | Delete
if mac:
[430] Fix | Delete
return mac
[431] Fix | Delete
return None
[432] Fix | Delete
[433] Fix | Delete
def _arp_getnode():
[434] Fix | Delete
"""Get the hardware address on Unix by running arp."""
[435] Fix | Delete
import os, socket
[436] Fix | Delete
try:
[437] Fix | Delete
ip_addr = socket.gethostbyname(socket.gethostname())
[438] Fix | Delete
except OSError:
[439] Fix | Delete
return None
[440] Fix | Delete
[441] Fix | Delete
# Try getting the MAC addr from arp based on our IP address (Solaris).
[442] Fix | Delete
mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: -1)
[443] Fix | Delete
if mac:
[444] Fix | Delete
return mac
[445] Fix | Delete
[446] Fix | Delete
# This works on OpenBSD
[447] Fix | Delete
mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: i+1)
[448] Fix | Delete
if mac:
[449] Fix | Delete
return mac
[450] Fix | Delete
[451] Fix | Delete
# This works on Linux, FreeBSD and NetBSD
[452] Fix | Delete
mac = _find_mac('arp', '-an', [os.fsencode('(%s)' % ip_addr)],
[453] Fix | Delete
lambda i: i+2)
[454] Fix | Delete
# Return None instead of 0.
[455] Fix | Delete
if mac:
[456] Fix | Delete
return mac
[457] Fix | Delete
return None
[458] Fix | Delete
[459] Fix | Delete
def _lanscan_getnode():
[460] Fix | Delete
"""Get the hardware address on Unix by running lanscan."""
[461] Fix | Delete
# This might work on HP-UX.
[462] Fix | Delete
return _find_mac('lanscan', '-ai', [b'lan0'], lambda i: 0)
[463] Fix | Delete
[464] Fix | Delete
def _netstat_getnode():
[465] Fix | Delete
"""Get the hardware address on Unix by running netstat."""
[466] Fix | Delete
# This might work on AIX, Tru64 UNIX.
[467] Fix | Delete
first_local_mac = None
[468] Fix | Delete
try:
[469] Fix | Delete
proc = _popen('netstat', '-ia')
[470] Fix | Delete
if not proc:
[471] Fix | Delete
return None
[472] Fix | Delete
with proc:
[473] Fix | Delete
words = proc.stdout.readline().rstrip().split()
[474] Fix | Delete
try:
[475] Fix | Delete
i = words.index(b'Address')
[476] Fix | Delete
except ValueError:
[477] Fix | Delete
return None
[478] Fix | Delete
for line in proc.stdout:
[479] Fix | Delete
try:
[480] Fix | Delete
words = line.rstrip().split()
[481] Fix | Delete
word = words[i]
[482] Fix | Delete
if len(word) == 17 and word.count(b':') == 5:
[483] Fix | Delete
mac = int(word.replace(b':', b''), 16)
[484] Fix | Delete
if _is_universal(mac):
[485] Fix | Delete
return mac
[486] Fix | Delete
first_local_mac = first_local_mac or mac
[487] Fix | Delete
except (ValueError, IndexError):
[488] Fix | Delete
pass
[489] Fix | Delete
except OSError:
[490] Fix | Delete
pass
[491] Fix | Delete
return first_local_mac or None
[492] Fix | Delete
[493] Fix | Delete
def _ipconfig_getnode():
[494] Fix | Delete
"""Get the hardware address on Windows by running ipconfig.exe."""
[495] Fix | Delete
import os, re, subprocess
[496] Fix | Delete
first_local_mac = None
[497] Fix | Delete
dirs = ['', r'c:\windows\system32', r'c:\winnt\system32']
[498] Fix | Delete
try:
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function