python 3: ipaddr/netaddr modules

MadSc13ntist picture MadSc13ntist · Apr 26, 2010 · Viewed 20.3k times · Source

I have got to be doing something wrong here... I am at present trying to validate whether an ip is within a specific subnet utilizing a builtin module.

I am using activepython:

ActivePython 3.1.2.3 (ActiveState Software Inc.) based on
Python 3.1.2 (r312:79147, Mar 22 2010, 12:20:29) [MSC v.1500 32 bit (Intel)] on win32

which has this in the changelog:

Python News
(editors: check NEWS.help for information about editing NEWS using ReST.)

What's New in Python 3.1.2?
Release date: 2010-03-20

----- snip -----

Removed the ipaddr module. 
Issue #3613: base64.{encode,decode}string are now called 
System Message: WARNING/2 (, line 706)

----- snip -----

Issue #3959: The ipaddr module has been added to the standard library. Contributed by Google.

that and other google searches have led me to believe that ipaddr was a builtin and yet:

>>> import ipaddr
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named ipaddr
>>> from ipaddr import *
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named ipaddr

so i figured i would install netaddr and attempt to use that and all i get from netaddr is:

>>> import netaddr
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python31\lib\site-packages\netaddr\__init__.py", line 18, in 
    from netaddr.ip import IPAddress, IPNetwork, IPRange, all_matching_cidrs, \
  File "C:\Python31\lib\site-packages\netaddr\ip\__init__.py", line 1877, in 
    IPV6_LOOPBACK = IPAddress('::1')
  File "C:\Python31\lib\site-packages\netaddr\ip\__init__.py", line 262, in __init__
    self.value = addr
  File "C:\Python31\lib\site-packages\netaddr\ip\__init__.py", line 292, in _set_value
    % value)
netaddr.core.AddrFormatError: failed to detect IP version: '::1'

I am feeling fairly frustrated and i'm not sure where to go from here... suggestions?

Answer

Thomas Wouters picture Thomas Wouters · Apr 26, 2010

the ipaddr module was added in the 3.1 development cycle (between alpha 2 and beta 1), but removed before the first release candidate of 3.1. It's not part of the standard library of any released Python version. You can still download and install it from, for example, PyPI.

The netaddr failure seems to be a bug in netaddr itself. It tries to detect IPv6 support and fails. I'd guess it's a bug in the module, but a fix or workaround is harder to guess at.