Skip to main content

Like ``ipaddress``, but for hardware identifiers such as MAC addresses.

Project description

A module for handling hardware identifiers like MAC addresses.

This module makes it easy to:

  1. check if a string represents a valid MAC address, or a similar hardware identifier like an EUI-64, OUI, etc,

  2. convert between string and binary forms of MAC addresses and other hardware identifiers,

and so on.

Heavily inspired by the ipaddress module, but not yet quite as featureful.

Versioning

This library’s version numbers follow the SemVer 2.0.0 specification.

Installation

pip install macaddress

Usage

Import:

import macaddress

Several classes are provided to parse common hardware addresses (MAC/EUI48, EUI64, OUI, etc), as well as several less common ones (EUI60, CDI32, etc). They each support several common formats.

For example, the EUI48 and MAC classes support the following formats:

>>> macaddress.MAC('01-23-45-67-89-ab')
MAC('01-23-45-67-89-AB')
>>> macaddress.MAC('01:23:45:67:89:ab')
MAC('01-23-45-67-89-AB')
>>> macaddress.MAC('0123.4567.89ab')
MAC('01-23-45-67-89-AB')
>>> macaddress.MAC('0123456789ab')
MAC('01-23-45-67-89-AB')

You can inspect what formats a hardware address class supports by looking at its formats attribute:

>>> macaddress.OUI.formats
('xx-xx-xx', 'xx:xx:xx', 'xxxxxx')

The first format listed in formats is also the one used when stringifying (str) or representing (repr) the object.

Most classes supplied by this module have the oui attribute, which just returns their first three bytes as an OUI object:

>>> macaddress.EUI48('01:02:03:04:05:06').oui
OUI('01-02-03')

All macaddress classes support equality comparisons:

>>> macaddress.OUI('01-02-03') == macaddress.OUI('01:02:03')
True
>>> macaddress.OUI('01-02-03') == macaddress.OUI('ff-ee-dd')
False
>>> macaddress.OUI('01-02-03') == macaddress.CDI32('01-02-03-04')
False
>>> macaddress.OUI('01-02-03') == macaddress.CDI32('01-02-03-04').oui
True

All macaddress classes can be initialized with raw bytes or raw integers representing their value instead of strings:

>>> macaddress.MAC(b'abcdef')
MAC('61-62-63-64-65-66')
>>> macaddress.MAC(0x010203ffeedd)
MAC('01-02-03-FF-EE-DD')
>>> macaddress.MAC(1)
MAC('00-00-00-00-00-01')
>>> macaddress.OUI(b'abc')
OUI('61-62-63')
>>> macaddress.OUI(0x010203)
OUI('01-02-03')
>>> macaddress.OUI(1)
OUI('00-00-01')

If any of the values passed to the constructors are invalid, the constructors raise a TypeError or a ValueError as appropriate.

All macaddress classes also support total ordering. The comparisons are intended to intuitively put identifiers that start with the same bits next to each other sorting:

>>> some_values = [
...     MAC('ff-ee-dd-01-02-03'),
...     MAC('ff-ee-00-99-88-77'),
...     MAC('ff-ee-dd-01-02-04'),
...     OUI('ff-ee-dd'),
... ]
>>> for x in sorted(some_values):
...     print(x)
FF-EE-00-01-02-03
FF-EE-DD
FF-EE-DD-01-02-03
FF-EE-DD-01-02-04

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

macaddress-1.0.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

macaddress-1.0.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file macaddress-1.0.0.tar.gz.

File metadata

  • Download URL: macaddress-1.0.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for macaddress-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6b4d7ec483e3f2ed41acc56562e02c4054d1ac3707022f3dbbe70ed213fa7ce2
MD5 b73b7cc293a82c13558b329d5e82bfa7
BLAKE2b-256 9d734e2df383e21e698964e6f74fb1c2286380f81e15bda82148cdc40071203a

See more details on using hashes here.

File details

Details for the file macaddress-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: macaddress-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for macaddress-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0d794c618bf27d68c1be678a266bcc705cdb4e64c599cc0b1ee95a49e6ba57d
MD5 f4e2d390e2dcc8b9e0b77bd5f17531f8
BLAKE2b-256 39d008a4bd6325364d7f38b322e8dc5e0eaccddbc4763a05b8dcf49e930ea592

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page