multiformats.multiaddr
Implementation of the multiaddr spec.
Suggested usage:
>>> from multiformats import multiaddr
Addr
- class Addr(proto, value)[source]
Bases:
object
Container class for a single protocol address in a multiaddr.
>>> a = Addr('ip4', '192.168.1.1') >>> a Addr('ip4', '192.168.1.1') >>> str(a) '/ip4/192.168.1.1'
The slash notation provides a more literate way to construct protocol addresses:
>>> a = ip4/"192.168.1.1" >>> a Addr('ip4', '192.168.1.1')
Bytes for protocol addresses are computed according to the TLV multiaddr format:
>>> bytes(ip4/"127.0.0.1").hex() '047f000001' >>> varint.encode(ip4.code).hex() '04' >>> bytes([127, 0, 0, 1]).hex() '7f000001'
- Parameters:
- property proto
The address protocol.
Example usage:
>>> a = Addr('ip4', '192.168.1.1') >>> a.proto Proto('ip4')
- Return type:
- property value
The address value, as a string.
Example usage:
>>> a = Addr('ip4', '192.168.1.1') >>> a.value '192.168.1.1'
- Return type:
Multiaddr
- class Multiaddr(*addrs)[source]
-
Container class for a multiaddr.
Example usage:
>>> ip4 = multiaddr.proto("ip4") >>> udp = multiaddr.proto("udp") >>> quic = multiaddr.proto("quic") >>> ma = ip4/"127.0.0.1"/udp/9090/quic >>> ma Multiaddr(Addr('ip4', '127.0.0.1'), Addr('udp', '9090'), Proto('quic')) >>> str(ma) '/ip4/127.0.0.1/udp/9090/quic'
Bytes for multiaddrs are computed according to the (TLV)+ multiaddr format:
>>> bytes(ip4/"127.0.0.1").hex() '047f000001' >>> bytes(udp/9090).hex() '91022382' >>> bytes(quic).hex() 'cc03' >>> bytes(ma).hex() '047f00000191022382cc03'
- Parameters:
addrs (sequence of
Proto
orAddr
) – a sequence of protocols (not requiring address) and protocol addresses- Raises:
ValueError – if a
Proto
instance appears at a place other than the last for a protocol requiring an addressValueError – if a procool name appears more than once
- index(value, start=0, stop=None)[source]
Returns the unique index at which a protocol/address appears in the multiaddress:
>>> ma = ip4/"127.0.0.1"/udp/9090/quic >>> str(ma) '/ip4/127.0.0.1/udp/9090/quic' >>> udp in ma True >>> ma.index(udp) 1 >>> ma[ma.index(udp)] Addr('udp', '9090') >>> ip4/"127.0.0.1" in ma True >>> ma.index(ip4/"127.0.0.1" in ma) 0
- Parameters:
>>> ip4/"127.0.0.1" in ma True >>> ma.index(ip4/"127.0.0.1") 0 >>> ma.index(ip4/"127.0.0.1", start=1) MultiaddrValueError: Address Addr('ip4', '127.0.0.1') does not appear in sub-multiaddr /udp/9090/quic of multiaddr /ip4/127.0.0.1/udp/9090/quic
- Raises:
ValueError – if the protocol/address does not appear:
>>> ip6 = Proto("ip6") >>> ip6 in ma False >>> ma.index(ip6) MultiaddrValueError: Protocol 'ip6' does not appear in multiaddr /ip4/127.0.0.1/udp/9090/quic >>> ip4/"127.0.0.2" in ma False >>> ma.index(ip4/"127.0.0.2") MultiaddrValueError: Address Addr('ip4', '127.0.0.2') does not appear in multiaddr /ip4/127.0.0.1/udp/9090/quic
- Return type:
- property is_incomplete
Whether this multiaddress is incomplete, i.e. it still requires an address for the last protocol in the sequence.
>>> ma = ip4/"127.0.0.1"/udp >>> ma.is_incomplete True >>> str(ma) '/ip4/127.0.0.1/udp' >>> ma2 = ma/9090 >>> str(ma2) '/ip4/127.0.0.1/udp/9090' >>> ma2.is_incomplete False
Incomplete multiaddrs don’t admit a byte representation:
>>> bytes(ma) MultiaddrValueError: Missing address value for last protocol, cannot compute bytes. >>> bytes(ma2).hex() '047f00000191022382'
- Return type:
Proto
- class Proto(codec)[source]
Bases:
object
Container class for a single protocol segment of a multiaddr.
>>> ip4 = Proto("ip4") >>> ip4 Proto("ip4") >>> str(ip4) '/ip4'
For protocols that don’t require an address value, bytes are computed as the varint encoding of protocol code:
>>> quic = Proto('quic') >>> quic.code 460 >>> varint.encode(quic.code).hex() 'cc03' >>> bytes(quic).hex() 'cc03'
- Parameters:
codec (
str
,int
orMulticodec
) – the multicodec for this protocol (by name, code, or as object)
- addr(value)[source]
Returns an address for this protocol.
Example usage:
>>> ip4.addr("192.168.1.1") Addr('ip4', '192.168.1.1') >>> ip4.addr(bytes([192, 168, 1, 1])) Addr('ip4', '192.168.1.1')
The same address can be obtained with slash syntax:
>>> ip4/"192.168.1.256" Addr('ip4', '192.168.1.256') >>> ip4/b'\xc0\xa8\x01\x01' Addr('ip4', '192.168.1.1')
- property addr_size
The address size (in bytes) for this protocol:
for protocols with no address,
addr_size
is 0for protocols with addresses of variable binary size,
addr_size
isNone
for all other protocols,
addr_size
is a positiveint
Example usage:
>>> ip4.addr_size 4
- property admits_addr
Whether this protocol admits an address.
>>> ip4.admits_addr True
- Return type:
- property codec
The multicodec for this protocol.
Example usage:
>>> ip4.codec Multicodec(name='ip4', tag='multiaddr', code='0x04', status='permanent', description='')
- Return type:
- property implementation
The implementation for this protocol, as a triple of raw encoder, raw decoder and address size.
Example usage:
>>> ip4.implementation ( <function ip4_encoder at 0x000002B4C9956310>, <function ip4_decoder at 0x000002B4C99563A0>, 4 )
- Return type:
- is_addr_valid(addr_value)[source]
Validates an address value.
Example usage:
>>> ip4.is_addr_valid("192.168.1.1") True >>> ip4.is_addr_valid(bytes([192, 168, 1, 1])) True
The same result can be obtained with container syntax:
>>> "192.168.1.1" in ip4 True >>> bytes([192, 168, 1, 1]) in ip4 True
- property raw_decoder
The raw decoder for this protocol.
Example usage:
>>> ip4.raw_decoder <function ip4_decoder at 0x000002B4C99563A0>
- Return type:
RawDecoder
orNone
- property raw_encoder
The raw encoder for this protocol.
Example usage:
>>> ip4.raw_encoder <function ip4_encoder at 0x000002B4C9956310>
- Return type:
RawEncoder
orNone
byteslike
- byteslike = (<class 'bytes'>, <class 'bytearray'>, <class 'memoryview'>)
Built-in immutable sequence.
If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable’s items.
If the argument is a tuple, the return value is the same object.
decode
parse
- parse(s, allow_incomplete=False)[source]
Parses a multiaddr from its human-readable string representation.
Example usage:
>>> s = '/ip4/127.0.0.1/udp/9090/quic' >>> multiaddr.parse(s) Multiaddr(Addr('ip4', '127.0.0.1'), Addr('udp', '9090'), Proto('quic'))
Example usage with incomplete multiaddr:
>>> s = '/ip4/127.0.0.1/udp' >>> multiaddr.parse(s) MultiaddrValueError: Decoded multiaddr is incomplete: /ip4/127.0.0.1/udp >>> multiaddr.parse(s, allow_incomplete=True) Multiaddr(Addr('ip4', '127.0.0.1'), Proto('udp'))
- Parameters:
- Raises:
ValueError – if
allow_incomplete
isFalse
and the parsed multiaddress is incomplete- Return type: