MAC Address Library

2.0.2 · active · verified Thu Apr 16

A module for handling hardware identifiers like MAC addresses, EUI-48, EUI-64, and OUI. Heavily inspired by Python's built-in `ipaddress` module, `macaddress` provides classes to check if a string represents a valid hardware address, convert between various string and binary forms, and allows users to define custom hardware address types. It adheres to the SemVer 2.0.0 specification, with the current stable version being 2.0.2.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create `EUI48` and `EUI64` objects, access various string representations, handle `ValueError` for invalid inputs, and define a custom hardware address type.

import macaddress

# Create an EUI48 (MAC) address object
mac = macaddress.EUI48('01-23-45-67-89-ab')
print(f"MAC Address: {mac}")

# Access different formats
print(f"MAC in colon format: {mac.colon}")
print(f"MAC in period format: {mac.period}")
print(f"MAC as bytes: {mac.packed}")

# Validate an address string
try:
    invalid_mac = macaddress.EUI48('foo bar')
except ValueError as e:
    print(f"Error parsing invalid MAC: {e}")

# Create an EUI64 address
eui64 = macaddress.EUI64('01-23-45-67-89-ab-cd-ef')
print(f"EUI64 Address: {eui64}")

# Define a custom format (example from docs)
class CustomMAC(macaddress.MAC):
    formats = macaddress.MAC.formats + (
        'xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx',
    )

custom_mac = CustomMAC('01-23-45-67-89-AB-01-23-45-67-89-AB-01-23-45-67-89-AB')
print(f"Custom MAC: {custom_mac}")

view raw JSON →