net.ip
import net
Everything here is re-exported by
net, soimport netis enough and the names are called asnet.*. Importingnet.ipon its own works too and reaches the same definitions.
This module provides types for parsing, formatting, comparing and classifying IP addresses in both their IPv4 and IPv6 forms.
Ipv4 and Ipv6 each represent an address of their respective family,
while Ip is a small wrapper around either one for code that needs to
accept and work with both interchangeably.
The example below parses an address of unknown family and reports a few things about it.
import net
var addr = net.Ip.parse('192.168.1.10')
if addr.is_private() {
echo '${addr} is a private address'
}
Ipv4 and Ipv6 can also be worked with directly when the family is
already known, as shown below.
import net
var a = net.Ipv4.parse('10.0.0.1')
var b = net.Ipv4(10, 0, 0, 2)
echo a.is_private()
echo a.equals(b)
echo a.octets()
Classes
Ipv4
class net.Ipv4
An IPv4 address, stored internally as four octets in network (i.e. big-endian, most significant octet first) order.
- printable — has a
@to_string(), soechoandprint()show something useful
Constructor
net.Ipv4(a, b, c, d)
Returns a new instance of an Ipv4 from its four octets, given
in the same left-to-right order they’d be written in dotted-decimal
notation, e.g. Ipv4(192, 168, 1, 10) for 192.168.1.10.
Parameters
a(number) — The 1st octet,0-255b(number) — The 2nd octet,0-255c(number) — The 3rd octet,0-255d(number) — The 4th octet,0-255
Raises Error if any octet is missing or outside 0-255
Ipv4.unspecified()
net.Ipv4.unspecified() -> Ipv4
The 0.0.0.0 address, used to mean “any local address” when binding a
socket.
Returns Ipv4
Ipv4.localhost()
net.Ipv4.localhost() -> Ipv4
The 127.0.0.1 loopback address.
Returns Ipv4
Ipv4.broadcast()
net.Ipv4.broadcast() -> Ipv4
The 255.255.255.255 limited broadcast address.
Returns Ipv4
Ipv4.parse()
net.Ipv4.parse(address) -> Ipv4
Parses an address given in standard dotted-decimal notation, e.g.
'192.168.1.10'.
Parameters
address(string) — The address to parse
Returns Ipv4
Raises Error if address is not four dot-separated octets, each a
decimal number from 0 to 255
Note: Each octet must be written without a leading zero (other than the literal digit
0itself) -'192.168.001.10'is rejected rather than guessed at, since a leading zero is read as an octal prefix by some other tools and silently accepting it here could make an address mean two different things depending on what reads it.
Ipv4.octets()
net.Ipv4.octets() -> number[]
Returns the four octets making up this address as a list, in the same left-to-right order they’d be written in dotted-decimal notation. Each call returns a fresh list, so mutating it does not affect this instance.
Returns number[] — [a, b, c, d]
Ipv4.is_unspecified()
net.Ipv4.is_unspecified() -> bool
Whether this is the 0.0.0.0 unspecified address.
Returns bool
Ipv4.is_loopback()
net.Ipv4.is_loopback() -> bool
Whether this address is in the 127.0.0.0/8 loopback range.
Returns bool
Ipv4.is_private()
net.Ipv4.is_private() -> bool
Whether this address is in one of the private-use ranges reserved by RFC
1918: 10.0.0.0/8, 172.16.0.0/12, or 192.168.0.0/16.
Returns bool
Ipv4.is_link_local()
net.Ipv4.is_link_local() -> bool
Whether this address is in the 169.254.0.0/16 link-local range, used
for address autoconfiguration when no other address is available.
Returns bool
Ipv4.is_multicast()
net.Ipv4.is_multicast() -> bool
Whether this address is in the 224.0.0.0/4 multicast range.
Returns bool
Ipv4.is_broadcast()
net.Ipv4.is_broadcast() -> bool
Whether this is the 255.255.255.255 limited broadcast address.
Returns bool
Ipv4.is_documentation()
net.Ipv4.is_documentation() -> bool
Whether this address falls in one of the ranges reserved by RFC 5737 for
use in documentation and examples: 192.0.2.0/24, 198.51.100.0/24, or
203.0.113.0/24.
Returns bool
Ipv4.to_ipv6_mapped()
net.Ipv4.to_ipv6_mapped() -> Ipv6
Returns this address re-expressed as an IPv4-mapped IPv6 address, e.g.
192.168.1.10 becomes ::ffff:192.168.1.10.
Returns Ipv6
Ipv4.equals()
net.Ipv4.equals(other) -> bool
Whether other refers to the same address as this one.
Parameters
other(Ipv4) — The address to compare against
Returns bool
Ipv4.to_string()
net.Ipv4.to_string() -> string
This address in standard dotted-decimal notation, e.g. '192.168.1.10'.
Returns string
Ipv4.to_list()
net.Ipv4.to_list() -> list
Returns the octets of the Ipv4 address as a list
Returns list
Ipv6
class net.Ipv6
An IPv6 address, stored internally as eight 16-bit segments in network (i.e. big-endian, most significant segment first) order.
- printable — has a
@to_string(), soechoandprint()show something useful
Constructor
net.Ipv6(a, b, c, d, e, f, g, h)
Returns a new instance of an Ipv6 from its eight segments,
given in the same left-to-right order they’d be written in colon-hex
notation, e.g. Ipv6(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1) for
2001:db8::1.
Parameters
a(number) — The 1st segment,0-65535b(number) — The 2nd segment,0-65535c(number) — The 3rd segment,0-65535d(number) — The 4th segment,0-65535e(number) — The 5th segment,0-65535f(number) — The 6th segment,0-65535g(number) — The 7th segment,0-65535h(number) — The 8th segment,0-65535
Raises Error if any segment is missing or outside 0-65535
Ipv6.unspecified()
net.Ipv6.unspecified() -> Ipv6
The :: unspecified address, used to mean “any local address” when
binding a socket.
Returns Ipv6
Ipv6.localhost()
net.Ipv6.localhost() -> Ipv6
The ::1 loopback address.
Returns Ipv6
Ipv6.parse()
net.Ipv6.parse(address) -> Ipv6
Parses an address given in standard colon-hex notation, including the
:: shorthand for a run of zero segments (at most one :: is allowed
per address) and the embedded dotted-decimal form used for
IPv4-mapped/-compatible addresses, e.g. '::ffff:192.168.1.10'.
Parameters
address(string) — The address to parse
Returns Ipv6
Raises Error if address is not a well-formed IPv6 address
Note: Zone identifiers (e.g. the
%eth0suffix used to disambiguate link-local addresses on a particular interface) are not supported and will cause parsing to fail.
Ipv6.segments()
net.Ipv6.segments() -> number[]
Returns the eight segments making up this address as a list, in the same left-to-right order they’d be written in colon-hex notation. Each call returns a fresh list, so mutating it does not affect this instance.
Returns number[] — [a, b, c, d, e, f, g, h]
Ipv6.is_unspecified()
net.Ipv6.is_unspecified() -> bool
Whether this is the :: unspecified address.
Returns bool
Ipv6.is_loopback()
net.Ipv6.is_loopback() -> bool
Whether this is the ::1 loopback address.
Returns bool
Ipv6.is_multicast()
net.Ipv6.is_multicast() -> bool
Whether this address is in the ff00::/8 multicast range.
Returns bool
Ipv6.is_unicast_link_local()
net.Ipv6.is_unicast_link_local() -> bool
Whether this address is in the fe80::/10 unicast link-local range.
Returns bool
Ipv6.to_ipv4_mapped()
net.Ipv6.to_ipv4_mapped() -> ?Ipv4
If this address is an IPv4-mapped address (::ffff:0:0/96), returns the
IPv4 address it maps to; otherwise returns nil.
Returns ?Ipv4
Ipv6.equals()
net.Ipv6.equals(other) -> bool
Whether other refers to the same address as this one.
Parameters
other(Ipv6) — The address to compare against
Returns bool
Ipv6.to_string()
net.Ipv6.to_string() -> string
This address in its canonical, most-compressed colon-hex notation - the
longest run of consecutive zero segments (if any run is at least two
segments long) is collapsed to ::; if more than one such run exists,
the leftmost is preferred.
Returns string
Ipv6.to_list()
net.Ipv6.to_list() -> list
Returns the segments of the Ipv6 address as a list
Returns list
Ip
class net.Ip
Either an Ipv4 or an Ipv6, for code that needs to
accept and work with addresses of either family without committing to
one upfront.
- printable — has a
@to_string(), soechoandprint()show something useful
Constructor
net.Ip(inner)
Wraps an already-constructed Ipv4 or Ipv6.
Application code should generally prefer Ip.parse(), Ip.v4(), or
Ip.v6() over calling this directly.
Parameters
inner(Ipv4|Ipv6) — The address to wrap
Ip.v4()
net.Ip.v4(address) -> Ip
Parameters
address(Ipv4) — The address to wrap
Returns Ip
Ip.v6()
net.Ip.v6(address) -> Ip
Parameters
address(Ipv6) — The address to wrap
Returns Ip
Ip.parse()
net.Ip.parse(address) -> Ip
Parses address as either an IPv4 or IPv6 address, choosing the family
based on whether address contains a : character.
Parameters
address(string) — The address to parse
Returns Ip
Raises Error if address is not a well-formed IPv4 or IPv6
address
Ip.is_ipv4()
net.Ip.is_ipv4() -> bool
Whether this address is an Ipv4.
Returns bool
Ip.is_ipv6()
net.Ip.is_ipv6() -> bool
Whether this address is an Ipv6.
Returns bool
Ip.as_ipv4()
net.Ip.as_ipv4() -> ?Ipv4
Returns the wrapped Ipv4, or nil if this is an IPv6
address.
Returns ?Ipv4
Ip.as_ipv6()
net.Ip.as_ipv6() -> ?Ipv6
Returns the wrapped Ipv6, or nil if this is an IPv4
address.
Returns ?Ipv6
Ip.is_unspecified()
net.Ip.is_unspecified() -> bool
Whether this is the unspecified address for its family (0.0.0.0 or
::).
Returns bool
Ip.is_loopback()
net.Ip.is_loopback() -> bool
Whether this is the loopback address for its family (127.0.0.0/8 or
::1).
Returns bool
Ip.is_multicast()
net.Ip.is_multicast() -> bool
Whether this is a multicast address for its family.
Returns bool
Ip.equals()
net.Ip.equals(other) -> bool
Whether other refers to the same address as this one. Two addresses of
different families are never equal, even if one is the IPv4-mapped form
of the other - compare their canonical forms explicitly if that’s the
comparison you want.
Parameters
other(Ip) — The address to compare against
Returns bool
Ip.to_string()
net.Ip.to_string() -> string
This address in its family’s standard notation.
Returns string
Ip.to_list()
net.Ip.to_list() -> list
Returns the address in this IP as a list. If the address is an Ipv4 address, the list will contain four (4) elements. Otherwise, it will contain eight (8) elements.
Returns list
2026, Richard Ore and Zuri contributors