MadSc13ntist
MadSc13ntist

Reputation: 21450

python 3: ipaddr/netaddr modules

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?

Upvotes: 4

Views: 27459

Answers (3)

Tomek
Tomek

Reputation: 1270

The module ipaddress is included in python 3.3 which is a new version of the ipaddr module.

ipaddress is backwards incompatible with the ipaddr module available on PyPI.

The main differences are:

  • ipaddress *Network classes are equivalent to the ipaddr *Network class counterparts with the strict flag set to True.
  • ipaddress *Interface classes are equivalent to the ipaddr *Network class counterparts with the strict flag set to False.
  • The factory functions in ipaddress were renamed to disambiguate them from classes.
  • A few attributes were renamed to disambiguate their purpose as well. (eg. network, network_address)
    • A number of methods and functions which returned containers in ipaddr now return iterators. This includes subnets, address_exclude, summarize_address_range and collapse_address_list.

For more information see PEP-3144.

Upvotes: 2

David Moss
David Moss

Reputation: 41

netaddr doesn't support Python 3.x yet (as mentioned in the README).

However, it will do in the forthcoming release (0.7.5). I'll take this as a hint that I should hurry up and get it out the door!

I've added a ticket on the project bug tracker here :-

http://code.google.com/p/netaddr/issues/detail?id=55

Thanks.

Upvotes: 4

Thomas Wouters
Thomas Wouters

Reputation: 133405

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.

Upvotes: 3

Related Questions