Multiaddr Python Implementation

0.2.0 · active · verified Wed Apr 15

Multiaddr is a Python library that provides an implementation of jbenet's multiaddr specification, designed for future-proof, composable, and efficient network addresses. It aims to solve ambiguities in traditional network addressing by making addresses self-describing, specifying both protocol and network address. The library supports various protocols like IPv4, IPv6, TCP, UDP, DNS, and more, offering both human-readable and machine-readable representations. It is currently at version 0.2.0 and receives active development and releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create Multiaddr instances from strings and bytes, retrieve the list of protocols, and perform encapsulation and decapsulation operations, which are core functionalities of the library.

from multiaddr import Multiaddr

# Construct a Multiaddr from a string
addr_str = "/ip4/127.0.0.1/tcp/8080"
m1 = Multiaddr(addr_str)
print(f"Multiaddr from string: {m1}")
print(f"Protocols: {[p.name for p in m1.protocols()]}")

# Construct from bytes
m2 = Multiaddr(m1.to_bytes())
print(f"Multiaddr from bytes: {m2}")

# Encapsulate another Multiaddr
websocket_addr = Multiaddr("/ws")
encapsulated_addr = m1.encapsulate(websocket_addr)
print(f"Encapsulated Multiaddr: {encapsulated_addr}")

# Decapsulate a protocol
decapsulated_addr = encapsulated_addr.decapsulate(websocket_addr)
print(f"Decapsulated Multiaddr: {decapsulated_addr}")

assert str(m1) == str(decapsulated_addr)

view raw JSON →