vinegar.transform.ip_address
Transformations for both IPv4 and IPv6 addresses.
- vinegar.transform.ip_address.net_address(value: str, raise_error_if_malformed: bool = False) str
Calculate the network address for an IPv4 or IPv6 address and subnet mask.
The subnet mask must be given as part of the input string (separated from the IP address by a forward slash) and is included in the returned net address.
For example, an input of “192.168.0.1/24” results in “192.168.0.0/24” and an input of “2001:db8::1/32” results in “2001:db8::/32”.
- Parameters:
value – input IP address and mask to be transformed.
raise_error_if_malformed –
Trueif a malformed IP address or mask should result in aValueErrorbeing raised,Falseif it should result in the input value being returned as is. The default isFalse.
- Returns:
network address for the specified IP address and mask.
- vinegar.transform.ip_address.normalize(value: str, raise_error_if_malformed: bool = False) str
Normalize an IPv4 or IPv6 address.
This function takes an IPv4 or IPv6 address (as a
str) and transforms it into a normalized form. For an IPv4 address, this means that each byte of the IP address is represented without leading zeros. For an IPv6 address, the normalized form of the address should be close to the format format as defined by RFC 5952, but the actual return value might differ slightly. IPv6 addresses of the form “::ffff:1.2.3.4” (which typically only occur when handling IPv4 connections on IPv6 sockets) are transformed to an IPv4 address and then normalized according to the regular rules. If a mask is specified (separated from the IP address by a forward slash), it is also transformed to not include any leading zeros.When a malformed IP address is encountered, the behavior depends on the
raise_error_if_malformedoption. If that option is set toTrue, aValueErroris raised. If it isFalse(the default), the input value is returned unchanged.- Parameters:
value – input IP address to be transformed.
raise_error_if_malformed –
Trueif a malformed IP address should result in aValueErrorbeing raised,Falseif it should result in the input value being returned as is. The default isFalse.
- Returns:
normalized form of the input IP address.
- vinegar.transform.ip_address.strip_mask(value: str, raise_error_if_malformed: bool = False) str
Strip a subnet mask from an IPv4 or IPv6 address (if present).
For example, for the input string “192.168.0.1/24”, this returns “192.168.0.1”, and for “2001:db8::1/32”, this returns “2001:db8::1”. If the input IP address does not specify a subnet mask, it is returned as is.
- Parameters:
value – input IP address to be transformed.
raise_error_if_malformed –
Trueif a malformed IP address should result in aValueErrorbeing raised,Falseif it should result in the input value being returned as is. The default isFalse.
- Returns:
input IP address with the subnet mask removed.