New ipaddress module in python3.3

Back in 2010 I packaged Google’s ipaddr module because I needed a light weight IP address manipulation library that supported both IPv4 and IPv6 and (at the time) python-subnettree was IPv4 only.  Well, ipaddr is all grown up now and included in python3.3 as the ipaddress manipulation module in the standard library.  You can find details, as well as some description of the differences, in PEP 3144.

I just converted one package that I’m upstream for to use either ipaddr (for python2.6/2.7/3.2) or ipaddress instead of some custom code.  It turned out to be pretty easy to make it work with either.  Other than the name, the only difference I ran into was the removal of the common, generic IPAddress and IPNetwork functions that are replaced by ip_address and ip_network.


-import ipaddr
+try:
+    import ipaddress
+except ImportError:
+    import ipaddr as ipaddress

–    address = ipaddr.IPAddress(ip)
–    if isinstance(address, ipaddr.IPv4Address):
+    try:
+        address = ipaddress.ip_address(ip)
+    except AttributeError:
+        address = ipaddress.IPAddress(ip)
+    if isinstance(address, ipaddress.IPv4Address):

Currently, python3-ipaddr has no reverse-dependencies in the archive (python-ipaddr does).  Once python3.2 is dropped from Jessie, I think I’ll drop the python3-ipaddr binary on the assumption people newly coding for python3.3 should use ipaddress.  The python-ipaddr module will stick around for use with python2.7.

About these ads

1 Response to “New ipaddress module in python3.3”


  1. 1 David Fischer May 26, 2013 at 06:21

    Thank you !

    In order to avoid try … catch block when using the IP address class you can even do the following:

    try:
    from ipaddress import ip_address
    except ImportError:
    from ipaddr import IPAddress as ip_address

    And then use ip_address class.

    I am currently solving this Python3 issue with ipaddr, mock, … for my pyutils Python library and your post helped me a lot.

    Have a nice week-end !


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: