Lightweight EUI-48/64 based hardware (MAC) address library.
Project description
Lightweight python library for EUI-48, EUI-64 based hardware (MAC) addresses.
Quick start & Example usage
Installing with pip
$ pip install hwaddress
Import Generic hwaddress objects
>>> from hwaddress import MAC, MAC_64, GUID
>>> MAC.verify('12:34:56:78:90:ab')
True
>>> MAC.verify('12-34-56-78-90-ab')
False
>>> mac = MAC('12:34:56:78:90:ab')
>>> mac
MAC(12:34:56:78:90:ab)
>>> str(mac)
'12:34:56:78:90:ab'
>>> mac.format(delimiter='-')
'12-34-56-78-90-ab'
>>> mac.int
20015998341291
>>> mac.hex
'0x1234567890ab'
>>> mac.binary
'0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 1010 1011'
>>> MAC_64.verify('12:34:56:78:90:ab')
False
>>> MAC_64.verify('12:34:56:78:90:ab:cd:ef')
True
>>> MAC_64('0x1234567890abcdef').format(group=4, upper=True)
'1234:5678:90AB:CDEF'
>>> GUID.verify('12345678-90ab-cdef-1234-567890abcdef')
True
>>> GUID.verify('1234-5678-90ab-cdef-1234-5678-90ab-cdef')
False
>>> guid = GUID('123-45678-90ab-cdef-1234-5678:90ab.cdef')
>>> guid
GUID(12345678-90ab-cdef-1234-567890abcdef)
>>> guid.format(':', 4)
'1234:5678:90ab:cdef:1234:5678:90ab:cdef'
Included Hardware Address Classes
Name |
Format |
Properties |
---|---|---|
MAC |
ff:ff:ff:ff:ff:ff |
|
MAC_64 |
ff:ff:ff:ff:ff:ff:ff:ff |
|
GUID |
ffffffff-ffff-ffff-ffff-ffffffffffff |
|
EUI_48 |
ff-ff-ff-ff-ff-ff |
oui, oui36, cid |
EUI_64 |
ff-ff-ff-ff-ff-ff-ff-ff |
oui, oui36, cid |
WWN |
ff:ff:ff:ff:ff:ff:ff:ff |
naa, oui |
WWNx |
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff |
naa, oui |
IB_LID |
0xffff |
|
IB_GUID |
ffff:ffff:ffff:ffff |
|
IB_GID |
ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff |
prefix, guid |
Common Classmethods/Methods/Properties
All classes inheriting from MAC will have the following methods, classmethos, and properties.
Name |
Type |
Returns |
Description |
---|---|---|---|
classmethod |
bool |
Verify that address conforms to formatting defined by class. |
|
method |
str |
Format address with given formatting options. |
|
property |
int |
Integer representation of address. |
|
property |
str |
Hexadecimal representation of address. |
|
property |
str |
Padded binary representation of each hex digit in address. |
>>> hwaddress.MAC.verify('12:34:56:78:90:ab')
True
>>> hwaddress.MAC.verify('1234.5678.90ab')
False
>>> mac = hwaddress.MAC('12:34:56:78:90:ab')
>>> mac
MAC(12:34:56:78:90:ab)
>>> str(mac)
'12:34:56:78:90:ab'
>>> mac.format('-')
'12-34-56-78-90-ab'
>>> mac.format('.', 4)
'1234.5678.90ab'
>>> mac.format(group=4, upper=True)
'1234:5678:90AB'
int
>>> mac.int
20015998341291
hex
>>> mac.hex
'0x1234567890ab'
binary
>>> mac.binary
'0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 1010 1011'
EUI Properties
Name |
Returns |
Description |
---|---|---|
oui |
OIU |
24-bit Organizationally Unique Identifier. |
cid |
CID |
24-bit Company ID. |
oui36 |
OUI36 |
36-bit Organizationally Unique Identifier. |
WWN Properties
Name |
Returns |
Description |
---|---|---|
naa |
str |
Network Address Authority. |
oui |
OUI |
24-bit Organizationally Unique Identifier. |
IB_GID Properties
Name |
Returns |
Description |
---|---|---|
prefix |
IB_GID_prefix |
64-bit IB_GID_prefix. |
guid |
IB_GUID |
Embedded 64-bit IB_GUID. |
Factory Functions
new_hwaddress_class
>>> from hwaddress import new_hwaddress_class
get_address_factory
Return a hwaddress object from objs tuple depending on the address passed as an argument.
>>> from hwaddress import get_address_factory, EUI_48, EUI_64
>>>
>>> hw_address = get_address_factory()
>>>
>>> hw_address('12:34:56:78:90:ab')
MAC(12:34:56:78:90:ab)
>>> hw_address('12:34:56:78:90:ab:cd:ef')
MAC_64(12:34:56:78:90:ab:cd:ef)
>>>
>>> eui_address = get_address_factory(EUI_48, EUI_64)
get_verifier
>>> from hwaddress import MAC, EUI_48, get_verifier
>>>
>>> class MyMAC(MAC):
... _len_ = 48
... _del_ = '.'
... _grp_ = 4
...
>>>
>>> my_verifier = get_verifier(MAC, EUI_48, MyMAC)
>>>
>>> my_verifier('12:34:56:78:90:ab')
True
>>> my_verifier('12-34-56-78-90-ab')
True
>>> my_verifier('1234.5678.90ab')
True
>>> my_verifier('12.34.56.78.90.ab')
False
>>> my_verifier('1234-5678-90ab')
False
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for hwaddress-0.0.1.dev2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbccc22656f7683865a84df650dbefe8784f45dc3c429d689095433b756a902e |
|
MD5 | bd94e554603d1a1e6b0cdb785e25c1b0 |
|
BLAKE2b-256 | 299bd2068100376b45774bef2381f4f8a2bf1d62d30a7e5c26e24478f03a637e |