Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/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
[47] Fix | Delete
__author__ = 'Ka-Ping Yee <ping@zesty.ca>'
[48] Fix | Delete
[49] Fix | Delete
RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
[50] Fix | Delete
'reserved for NCS compatibility', 'specified in RFC 4122',
[51] Fix | Delete
'reserved for Microsoft compatibility', 'reserved for future definition']
[52] Fix | Delete
[53] Fix | Delete
int_ = int # The built-in int type
[54] Fix | Delete
bytes_ = bytes # The built-in bytes type
[55] Fix | Delete
[56] Fix | Delete
class UUID(object):
[57] Fix | Delete
"""Instances of the UUID class represent UUIDs as specified in RFC 4122.
[58] Fix | Delete
UUID objects are immutable, hashable, and usable as dictionary keys.
[59] Fix | Delete
Converting a UUID to a string with str() yields something in the form
[60] Fix | Delete
'12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts
[61] Fix | Delete
five possible forms: a similar string of hexadecimal digits, or a tuple
[62] Fix | Delete
of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and
[63] Fix | Delete
48-bit values respectively) as an argument named 'fields', or a string
[64] Fix | Delete
of 16 bytes (with all the integer fields in big-endian order) as an
[65] Fix | Delete
argument named 'bytes', or a string of 16 bytes (with the first three
[66] Fix | Delete
fields in little-endian order) as an argument named 'bytes_le', or a
[67] Fix | Delete
single 128-bit integer as an argument named 'int'.
[68] Fix | Delete
[69] Fix | Delete
UUIDs have these read-only attributes:
[70] Fix | Delete
[71] Fix | Delete
bytes the UUID as a 16-byte string (containing the six
[72] Fix | Delete
integer fields in big-endian byte order)
[73] Fix | Delete
[74] Fix | Delete
bytes_le the UUID as a 16-byte string (with time_low, time_mid,
[75] Fix | Delete
and time_hi_version in little-endian byte order)
[76] Fix | Delete
[77] Fix | Delete
fields a tuple of the six integer fields of the UUID,
[78] Fix | Delete
which are also available as six individual attributes
[79] Fix | Delete
and two derived attributes:
[80] Fix | Delete
[81] Fix | Delete
time_low the first 32 bits of the UUID
[82] Fix | Delete
time_mid the next 16 bits of the UUID
[83] Fix | Delete
time_hi_version the next 16 bits of the UUID
[84] Fix | Delete
clock_seq_hi_variant the next 8 bits of the UUID
[85] Fix | Delete
clock_seq_low the next 8 bits of the UUID
[86] Fix | Delete
node the last 48 bits of the UUID
[87] Fix | Delete
[88] Fix | Delete
time the 60-bit timestamp
[89] Fix | Delete
clock_seq the 14-bit sequence number
[90] Fix | Delete
[91] Fix | Delete
hex the UUID as a 32-character hexadecimal string
[92] Fix | Delete
[93] Fix | Delete
int the UUID as a 128-bit integer
[94] Fix | Delete
[95] Fix | Delete
urn the UUID as a URN as specified in RFC 4122
[96] Fix | Delete
[97] Fix | Delete
variant the UUID variant (one of the constants RESERVED_NCS,
[98] Fix | Delete
RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)
[99] Fix | Delete
[100] Fix | Delete
version the UUID version number (1 through 5, meaningful only
[101] Fix | Delete
when the variant is RFC_4122)
[102] Fix | Delete
"""
[103] Fix | Delete
[104] Fix | Delete
def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
[105] Fix | Delete
int=None, version=None):
[106] Fix | Delete
r"""Create a UUID from either a string of 32 hexadecimal digits,
[107] Fix | Delete
a string of 16 bytes as the 'bytes' argument, a string of 16 bytes
[108] Fix | Delete
in little-endian order as the 'bytes_le' argument, a tuple of six
[109] Fix | Delete
integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version,
[110] Fix | Delete
8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
[111] Fix | Delete
the 'fields' argument, or a single 128-bit integer as the 'int'
[112] Fix | Delete
argument. When a string of hex digits is given, curly braces,
[113] Fix | Delete
hyphens, and a URN prefix are all optional. For example, these
[114] Fix | Delete
expressions all yield the same UUID:
[115] Fix | Delete
[116] Fix | Delete
UUID('{12345678-1234-5678-1234-567812345678}')
[117] Fix | Delete
UUID('12345678123456781234567812345678')
[118] Fix | Delete
UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
[119] Fix | Delete
UUID(bytes='\x12\x34\x56\x78'*4)
[120] Fix | Delete
UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
[121] Fix | Delete
'\x12\x34\x56\x78\x12\x34\x56\x78')
[122] Fix | Delete
UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
[123] Fix | Delete
UUID(int=0x12345678123456781234567812345678)
[124] Fix | Delete
[125] Fix | Delete
Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must
[126] Fix | Delete
be given. The 'version' argument is optional; if given, the resulting
[127] Fix | Delete
UUID will have its variant and version set according to RFC 4122,
[128] Fix | Delete
overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.
[129] Fix | Delete
"""
[130] Fix | Delete
[131] Fix | Delete
if [hex, bytes, bytes_le, fields, int].count(None) != 4:
[132] Fix | Delete
raise TypeError('one of the hex, bytes, bytes_le, fields, '
[133] Fix | Delete
'or int arguments must be given')
[134] Fix | Delete
if hex is not None:
[135] Fix | Delete
hex = hex.replace('urn:', '').replace('uuid:', '')
[136] Fix | Delete
hex = hex.strip('{}').replace('-', '')
[137] Fix | Delete
if len(hex) != 32:
[138] Fix | Delete
raise ValueError('badly formed hexadecimal UUID string')
[139] Fix | Delete
int = int_(hex, 16)
[140] Fix | Delete
if bytes_le is not None:
[141] Fix | Delete
if len(bytes_le) != 16:
[142] Fix | Delete
raise ValueError('bytes_le is not a 16-char string')
[143] Fix | Delete
bytes = (bytes_le[4-1::-1] + bytes_le[6-1:4-1:-1] +
[144] Fix | Delete
bytes_le[8-1:6-1:-1] + bytes_le[8:])
[145] Fix | Delete
if bytes is not None:
[146] Fix | Delete
if len(bytes) != 16:
[147] Fix | Delete
raise ValueError('bytes is not a 16-char string')
[148] Fix | Delete
assert isinstance(bytes, bytes_), repr(bytes)
[149] Fix | Delete
int = int_.from_bytes(bytes, byteorder='big')
[150] Fix | Delete
if fields is not None:
[151] Fix | Delete
if len(fields) != 6:
[152] Fix | Delete
raise ValueError('fields is not a 6-tuple')
[153] Fix | Delete
(time_low, time_mid, time_hi_version,
[154] Fix | Delete
clock_seq_hi_variant, clock_seq_low, node) = fields
[155] Fix | Delete
if not 0 <= time_low < 1<<32:
[156] Fix | Delete
raise ValueError('field 1 out of range (need a 32-bit value)')
[157] Fix | Delete
if not 0 <= time_mid < 1<<16:
[158] Fix | Delete
raise ValueError('field 2 out of range (need a 16-bit value)')
[159] Fix | Delete
if not 0 <= time_hi_version < 1<<16:
[160] Fix | Delete
raise ValueError('field 3 out of range (need a 16-bit value)')
[161] Fix | Delete
if not 0 <= clock_seq_hi_variant < 1<<8:
[162] Fix | Delete
raise ValueError('field 4 out of range (need an 8-bit value)')
[163] Fix | Delete
if not 0 <= clock_seq_low < 1<<8:
[164] Fix | Delete
raise ValueError('field 5 out of range (need an 8-bit value)')
[165] Fix | Delete
if not 0 <= node < 1<<48:
[166] Fix | Delete
raise ValueError('field 6 out of range (need a 48-bit value)')
[167] Fix | Delete
clock_seq = (clock_seq_hi_variant << 8) | clock_seq_low
[168] Fix | Delete
int = ((time_low << 96) | (time_mid << 80) |
[169] Fix | Delete
(time_hi_version << 64) | (clock_seq << 48) | node)
[170] Fix | Delete
if int is not None:
[171] Fix | Delete
if not 0 <= int < 1<<128:
[172] Fix | Delete
raise ValueError('int is out of range (need a 128-bit value)')
[173] Fix | Delete
if version is not None:
[174] Fix | Delete
if not 1 <= version <= 5:
[175] Fix | Delete
raise ValueError('illegal version number')
[176] Fix | Delete
# Set the variant to RFC 4122.
[177] Fix | Delete
int &= ~(0xc000 << 48)
[178] Fix | Delete
int |= 0x8000 << 48
[179] Fix | Delete
# Set the version number.
[180] Fix | Delete
int &= ~(0xf000 << 64)
[181] Fix | Delete
int |= version << 76
[182] Fix | Delete
self.__dict__['int'] = int
[183] Fix | Delete
[184] Fix | Delete
def __eq__(self, other):
[185] Fix | Delete
if isinstance(other, UUID):
[186] Fix | Delete
return self.int == other.int
[187] Fix | Delete
return NotImplemented
[188] Fix | Delete
[189] Fix | Delete
# Q. What's the value of being able to sort UUIDs?
[190] Fix | Delete
# A. Use them as keys in a B-Tree or similar mapping.
[191] Fix | Delete
[192] Fix | Delete
def __lt__(self, other):
[193] Fix | Delete
if isinstance(other, UUID):
[194] Fix | Delete
return self.int < other.int
[195] Fix | Delete
return NotImplemented
[196] Fix | Delete
[197] Fix | Delete
def __gt__(self, other):
[198] Fix | Delete
if isinstance(other, UUID):
[199] Fix | Delete
return self.int > other.int
[200] Fix | Delete
return NotImplemented
[201] Fix | Delete
[202] Fix | Delete
def __le__(self, other):
[203] Fix | Delete
if isinstance(other, UUID):
[204] Fix | Delete
return self.int <= other.int
[205] Fix | Delete
return NotImplemented
[206] Fix | Delete
[207] Fix | Delete
def __ge__(self, other):
[208] Fix | Delete
if isinstance(other, UUID):
[209] Fix | Delete
return self.int >= other.int
[210] Fix | Delete
return NotImplemented
[211] Fix | Delete
[212] Fix | Delete
def __hash__(self):
[213] Fix | Delete
return hash(self.int)
[214] Fix | Delete
[215] Fix | Delete
def __int__(self):
[216] Fix | Delete
return self.int
[217] Fix | Delete
[218] Fix | Delete
def __repr__(self):
[219] Fix | Delete
return '%s(%r)' % (self.__class__.__name__, str(self))
[220] Fix | Delete
[221] Fix | Delete
def __setattr__(self, name, value):
[222] Fix | Delete
raise TypeError('UUID objects are immutable')
[223] Fix | Delete
[224] Fix | Delete
def __str__(self):
[225] Fix | Delete
hex = '%032x' % self.int
[226] Fix | Delete
return '%s-%s-%s-%s-%s' % (
[227] Fix | Delete
hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
[228] Fix | Delete
[229] Fix | Delete
@property
[230] Fix | Delete
def bytes(self):
[231] Fix | Delete
return self.int.to_bytes(16, 'big')
[232] Fix | Delete
[233] Fix | Delete
@property
[234] Fix | Delete
def bytes_le(self):
[235] Fix | Delete
bytes = self.bytes
[236] Fix | Delete
return (bytes[4-1::-1] + bytes[6-1:4-1:-1] + bytes[8-1:6-1:-1] +
[237] Fix | Delete
bytes[8:])
[238] Fix | Delete
[239] Fix | Delete
@property
[240] Fix | Delete
def fields(self):
[241] Fix | Delete
return (self.time_low, self.time_mid, self.time_hi_version,
[242] Fix | Delete
self.clock_seq_hi_variant, self.clock_seq_low, self.node)
[243] Fix | Delete
[244] Fix | Delete
@property
[245] Fix | Delete
def time_low(self):
[246] Fix | Delete
return self.int >> 96
[247] Fix | Delete
[248] Fix | Delete
@property
[249] Fix | Delete
def time_mid(self):
[250] Fix | Delete
return (self.int >> 80) & 0xffff
[251] Fix | Delete
[252] Fix | Delete
@property
[253] Fix | Delete
def time_hi_version(self):
[254] Fix | Delete
return (self.int >> 64) & 0xffff
[255] Fix | Delete
[256] Fix | Delete
@property
[257] Fix | Delete
def clock_seq_hi_variant(self):
[258] Fix | Delete
return (self.int >> 56) & 0xff
[259] Fix | Delete
[260] Fix | Delete
@property
[261] Fix | Delete
def clock_seq_low(self):
[262] Fix | Delete
return (self.int >> 48) & 0xff
[263] Fix | Delete
[264] Fix | Delete
@property
[265] Fix | Delete
def time(self):
[266] Fix | Delete
return (((self.time_hi_version & 0x0fff) << 48) |
[267] Fix | Delete
(self.time_mid << 32) | self.time_low)
[268] Fix | Delete
[269] Fix | Delete
@property
[270] Fix | Delete
def clock_seq(self):
[271] Fix | Delete
return (((self.clock_seq_hi_variant & 0x3f) << 8) |
[272] Fix | Delete
self.clock_seq_low)
[273] Fix | Delete
[274] Fix | Delete
@property
[275] Fix | Delete
def node(self):
[276] Fix | Delete
return self.int & 0xffffffffffff
[277] Fix | Delete
[278] Fix | Delete
@property
[279] Fix | Delete
def hex(self):
[280] Fix | Delete
return '%032x' % self.int
[281] Fix | Delete
[282] Fix | Delete
@property
[283] Fix | Delete
def urn(self):
[284] Fix | Delete
return 'urn:uuid:' + str(self)
[285] Fix | Delete
[286] Fix | Delete
@property
[287] Fix | Delete
def variant(self):
[288] Fix | Delete
if not self.int & (0x8000 << 48):
[289] Fix | Delete
return RESERVED_NCS
[290] Fix | Delete
elif not self.int & (0x4000 << 48):
[291] Fix | Delete
return RFC_4122
[292] Fix | Delete
elif not self.int & (0x2000 << 48):
[293] Fix | Delete
return RESERVED_MICROSOFT
[294] Fix | Delete
else:
[295] Fix | Delete
return RESERVED_FUTURE
[296] Fix | Delete
[297] Fix | Delete
@property
[298] Fix | Delete
def version(self):
[299] Fix | Delete
# The version bits are only meaningful for RFC 4122 UUIDs.
[300] Fix | Delete
if self.variant == RFC_4122:
[301] Fix | Delete
return int((self.int >> 76) & 0xf)
[302] Fix | Delete
[303] Fix | Delete
def _popen(command, *args):
[304] Fix | Delete
import os, shutil, subprocess
[305] Fix | Delete
executable = shutil.which(command)
[306] Fix | Delete
if executable is None:
[307] Fix | Delete
path = os.pathsep.join(('/sbin', '/usr/sbin'))
[308] Fix | Delete
executable = shutil.which(command, path=path)
[309] Fix | Delete
if executable is None:
[310] Fix | Delete
return None
[311] Fix | Delete
# LC_ALL=C to ensure English output, stderr=DEVNULL to prevent output
[312] Fix | Delete
# on stderr (Note: we don't have an example where the words we search
[313] Fix | Delete
# for are actually localized, but in theory some system could do so.)
[314] Fix | Delete
env = dict(os.environ)
[315] Fix | Delete
env['LC_ALL'] = 'C'
[316] Fix | Delete
proc = subprocess.Popen((executable,) + args,
[317] Fix | Delete
stdout=subprocess.PIPE,
[318] Fix | Delete
stderr=subprocess.DEVNULL,
[319] Fix | Delete
env=env)
[320] Fix | Delete
return proc
[321] Fix | Delete
[322] Fix | Delete
def _find_mac(command, args, hw_identifiers, get_index):
[323] Fix | Delete
try:
[324] Fix | Delete
proc = _popen(command, *args.split())
[325] Fix | Delete
if not proc:
[326] Fix | Delete
return
[327] Fix | Delete
with proc:
[328] Fix | Delete
for line in proc.stdout:
[329] Fix | Delete
words = line.lower().rstrip().split()
[330] Fix | Delete
for i in range(len(words)):
[331] Fix | Delete
if words[i] in hw_identifiers:
[332] Fix | Delete
try:
[333] Fix | Delete
word = words[get_index(i)]
[334] Fix | Delete
mac = int(word.replace(b':', b''), 16)
[335] Fix | Delete
if mac:
[336] Fix | Delete
return mac
[337] Fix | Delete
except (ValueError, IndexError):
[338] Fix | Delete
# Virtual interfaces, such as those provided by
[339] Fix | Delete
# VPNs, do not have a colon-delimited MAC address
[340] Fix | Delete
# as expected, but a 16-byte HWAddr separated by
[341] Fix | Delete
# dashes. These should be ignored in favor of a
[342] Fix | Delete
# real MAC address
[343] Fix | Delete
pass
[344] Fix | Delete
except OSError:
[345] Fix | Delete
pass
[346] Fix | Delete
[347] Fix | Delete
def _ifconfig_getnode():
[348] Fix | Delete
"""Get the hardware address on Unix by running ifconfig."""
[349] Fix | Delete
# This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
[350] Fix | Delete
keywords = (b'hwaddr', b'ether', b'address:', b'lladdr')
[351] Fix | Delete
for args in ('', '-a', '-av'):
[352] Fix | Delete
mac = _find_mac('ifconfig', args, keywords, lambda i: i+1)
[353] Fix | Delete
if mac:
[354] Fix | Delete
return mac
[355] Fix | Delete
[356] Fix | Delete
def _ip_getnode():
[357] Fix | Delete
"""Get the hardware address on Unix by running ip."""
[358] Fix | Delete
# This works on Linux with iproute2.
[359] Fix | Delete
mac = _find_mac('ip', 'link', [b'link/ether'], lambda i: i+1)
[360] Fix | Delete
if mac:
[361] Fix | Delete
return mac
[362] Fix | Delete
[363] Fix | Delete
def _arp_getnode():
[364] Fix | Delete
"""Get the hardware address on Unix by running arp."""
[365] Fix | Delete
import os, socket
[366] Fix | Delete
try:
[367] Fix | Delete
ip_addr = socket.gethostbyname(socket.gethostname())
[368] Fix | Delete
except OSError:
[369] Fix | Delete
return None
[370] Fix | Delete
[371] Fix | Delete
# Try getting the MAC addr from arp based on our IP address (Solaris).
[372] Fix | Delete
mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: -1)
[373] Fix | Delete
if mac:
[374] Fix | Delete
return mac
[375] Fix | Delete
[376] Fix | Delete
# This works on OpenBSD
[377] Fix | Delete
mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: i+1)
[378] Fix | Delete
if mac:
[379] Fix | Delete
return mac
[380] Fix | Delete
[381] Fix | Delete
# This works on Linux, FreeBSD and NetBSD
[382] Fix | Delete
mac = _find_mac('arp', '-an', [os.fsencode('(%s)' % ip_addr)],
[383] Fix | Delete
lambda i: i+2)
[384] Fix | Delete
if mac:
[385] Fix | Delete
return mac
[386] Fix | Delete
[387] Fix | Delete
def _lanscan_getnode():
[388] Fix | Delete
"""Get the hardware address on Unix by running lanscan."""
[389] Fix | Delete
# This might work on HP-UX.
[390] Fix | Delete
return _find_mac('lanscan', '-ai', [b'lan0'], lambda i: 0)
[391] Fix | Delete
[392] Fix | Delete
def _netstat_getnode():
[393] Fix | Delete
"""Get the hardware address on Unix by running netstat."""
[394] Fix | Delete
# This might work on AIX, Tru64 UNIX and presumably on IRIX.
[395] Fix | Delete
try:
[396] Fix | Delete
proc = _popen('netstat', '-ia')
[397] Fix | Delete
if not proc:
[398] Fix | Delete
return
[399] Fix | Delete
with proc:
[400] Fix | Delete
words = proc.stdout.readline().rstrip().split()
[401] Fix | Delete
try:
[402] Fix | Delete
i = words.index(b'Address')
[403] Fix | Delete
except ValueError:
[404] Fix | Delete
return
[405] Fix | Delete
for line in proc.stdout:
[406] Fix | Delete
try:
[407] Fix | Delete
words = line.rstrip().split()
[408] Fix | Delete
word = words[i]
[409] Fix | Delete
if len(word) == 17 and word.count(b':') == 5:
[410] Fix | Delete
mac = int(word.replace(b':', b''), 16)
[411] Fix | Delete
if mac:
[412] Fix | Delete
return mac
[413] Fix | Delete
except (ValueError, IndexError):
[414] Fix | Delete
pass
[415] Fix | Delete
except OSError:
[416] Fix | Delete
pass
[417] Fix | Delete
[418] Fix | Delete
def _ipconfig_getnode():
[419] Fix | Delete
"""Get the hardware address on Windows by running ipconfig.exe."""
[420] Fix | Delete
import os, re, subprocess
[421] Fix | Delete
dirs = ['', r'c:\windows\system32', r'c:\winnt\system32']
[422] Fix | Delete
try:
[423] Fix | Delete
import ctypes
[424] Fix | Delete
buffer = ctypes.create_string_buffer(300)
[425] Fix | Delete
ctypes.windll.kernel32.GetSystemDirectoryA(buffer, 300)
[426] Fix | Delete
dirs.insert(0, buffer.value.decode('mbcs'))
[427] Fix | Delete
except:
[428] Fix | Delete
pass
[429] Fix | Delete
for dir in dirs:
[430] Fix | Delete
try:
[431] Fix | Delete
proc = subprocess.Popen([os.path.join(dir, 'ipconfig'), '/all'],
[432] Fix | Delete
stdout=subprocess.PIPE,
[433] Fix | Delete
encoding="oem")
[434] Fix | Delete
except OSError:
[435] Fix | Delete
continue
[436] Fix | Delete
with proc:
[437] Fix | Delete
for line in proc.stdout:
[438] Fix | Delete
value = line.split(':')[-1].strip().lower()
[439] Fix | Delete
if re.fullmatch('(?:[0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value):
[440] Fix | Delete
return int(value.replace('-', ''), 16)
[441] Fix | Delete
[442] Fix | Delete
def _netbios_getnode():
[443] Fix | Delete
"""Get the hardware address on Windows using NetBIOS calls.
[444] Fix | Delete
See http://support.microsoft.com/kb/118623 for details."""
[445] Fix | Delete
import win32wnet, netbios
[446] Fix | Delete
ncb = netbios.NCB()
[447] Fix | Delete
ncb.Command = netbios.NCBENUM
[448] Fix | Delete
ncb.Buffer = adapters = netbios.LANA_ENUM()
[449] Fix | Delete
adapters._pack()
[450] Fix | Delete
if win32wnet.Netbios(ncb) != 0:
[451] Fix | Delete
return
[452] Fix | Delete
adapters._unpack()
[453] Fix | Delete
for i in range(adapters.length):
[454] Fix | Delete
ncb.Reset()
[455] Fix | Delete
ncb.Command = netbios.NCBRESET
[456] Fix | Delete
ncb.Lana_num = ord(adapters.lana[i])
[457] Fix | Delete
if win32wnet.Netbios(ncb) != 0:
[458] Fix | Delete
continue
[459] Fix | Delete
ncb.Reset()
[460] Fix | Delete
ncb.Command = netbios.NCBASTAT
[461] Fix | Delete
ncb.Lana_num = ord(adapters.lana[i])
[462] Fix | Delete
ncb.Callname = '*'.ljust(16)
[463] Fix | Delete
ncb.Buffer = status = netbios.ADAPTER_STATUS()
[464] Fix | Delete
if win32wnet.Netbios(ncb) != 0:
[465] Fix | Delete
continue
[466] Fix | Delete
status._unpack()
[467] Fix | Delete
bytes = status.adapter_address[:6]
[468] Fix | Delete
if len(bytes) != 6:
[469] Fix | Delete
continue
[470] Fix | Delete
return int.from_bytes(bytes, 'big')
[471] Fix | Delete
[472] Fix | Delete
# Thanks to Thomas Heller for ctypes and for his help with its use here.
[473] Fix | Delete
[474] Fix | Delete
# If ctypes is available, use it to find system routines for UUID generation.
[475] Fix | Delete
# XXX This makes the module non-thread-safe!
[476] Fix | Delete
_uuid_generate_time = _UuidCreate = None
[477] Fix | Delete
try:
[478] Fix | Delete
import ctypes, ctypes.util
[479] Fix | Delete
import sys
[480] Fix | Delete
[481] Fix | Delete
# The uuid_generate_* routines are provided by libuuid on at least
[482] Fix | Delete
# Linux and FreeBSD, and provided by libc on Mac OS X.
[483] Fix | Delete
_libnames = ['uuid']
[484] Fix | Delete
if not sys.platform.startswith('win'):
[485] Fix | Delete
_libnames.append('c')
[486] Fix | Delete
for libname in _libnames:
[487] Fix | Delete
try:
[488] Fix | Delete
lib = ctypes.CDLL(ctypes.util.find_library(libname))
[489] Fix | Delete
except Exception:
[490] Fix | Delete
continue
[491] Fix | Delete
if hasattr(lib, 'uuid_generate_time'):
[492] Fix | Delete
_uuid_generate_time = lib.uuid_generate_time
[493] Fix | Delete
break
[494] Fix | Delete
del _libnames
[495] Fix | Delete
[496] Fix | Delete
# The uuid_generate_* functions are broken on MacOS X 10.5, as noted
[497] Fix | Delete
# in issue #8621 the function generates the same sequence of values
[498] Fix | Delete
# in the parent process and all children created using fork (unless
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function