netaddr / netaddr

A network address manipulation library for Python

Home Page:https://netaddr.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[feature request] Type compatibility with ipaddress

johnnybubonic opened this issue · comments

Hi! With python 3.x (and MAYBE 2.7?), ipaddress is now a standard library.

It'd be great if netaddr could parse those objects into native objects(A) and have a native library object attribute(B), e.g.:

#!/usr/bin/env python3

import ipaddress
import netaddr

ip4 = ipaddress.ip_address('192.0.2.1')  # instance of ipaddress.IPv4Address
ip6 = ipaddress.ip_address('2001:DB8::1')  # instance of ipaddress.IPv6Address
net4 = ipaddress.ip_network('192.0.2.0/24')  # instance of ipaddress.IPv4Network
net6 = ipaddress.ip_network('2001:DB8::/32')  # instance of ipaddress.IPv6Network

# "A"
n_ip4 = netaddr.IPAddress(ip4)  # instance of netaddr.ip.IPAddress using above
n_ip6 = netaddr.IPAddress(ip6)  # instance of netaddr.ip.IPAddress using above
n_net4 = netaddr.IPNetwork(net4)  # instance of netaddr.ip.IPNetwork using above
n_net6 = netaddr.IPNetwork(net6)  # instance of netaddr.ip.IPNetwork using above

# "B"
isinstance(n_ip4.ipaddress, ipaddress.IPv4Address)  # This attribute would exist and this test would be True.

Thoughts?

A should be fairly easy; the str() conversion for ipaddress.IPv4Address/ipaddress.IPv6Address is the string representation that netaddr.IPAddress() accepts.

LIkewise, for ipaddress.IPv4Network/ipaddress.IPv6Network str() conversions, it's in the standard network/prefix format (e.g. 192.0.2.0/24).

I agree, looks convenient.

Since ipaddress is in the standard library, how about removing the impl duplicating the impl there and just reuse the impl that is already in the standard library?

I'm happy to accept a pull request that keeps the current interface but reuses the standard library implementation (Python 3.6+ is fine as we're about to drop 2.7 and 3.5 compatibility anyway).