Python 3: ipaddr / netaddr modules

I must be doing something wrong here ... I am currently trying to check if the ip is on a specific subnet using the built-in 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 PM) [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 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 other google searches have led me to believe that ipaddr was built in and also:

>>> 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 installed netaddr and try to use it 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 feel pretty frustrated and I'm not sure where to go from here ... suggestions?

+2


a source to share


3 answers


the module ipaddr

was added in the 3.1 development cycle (between alpha 2 and beta 1), but was removed before the first candidate for the 3.1 release. It is not included in the standard library of any released version of Python. You can download and install it like PyPI .



The error netaddr

seems to be a bug in netaddr

. It tries to detect IPv6 support and fails. I am guessing this is a bug in the module, but the fix or workaround is more difficult to guess.

+3


a source


netaddr does not yet support Python 3.x (as stated in the README).

However, this will be done in an upcoming release (0.7.5). I'll take this as a hint that I should hurry up and get her out the door!

I have added a ticket to the project bug tracker here: -



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

Thanks.

+3


a source


The module is ipaddress

included in python 3.3, which is the newer version of the module ipaddr

.

ipaddress

is backwards incompatible with the module ipaddr

available in PyPI.

The main differences are:

  • ipaddress * Network classes are equivalent to IPaddr * Network class combinations with strong flag set to True.
  • ipaddress * Interface classes are equivalent to IPaddr * Network class mates with strong flag set to False.
  • The factory functions in ipaddress have been renamed to remove them from classes.
  • Several attributes have been renamed to eliminate their purpose. (e.g. network, network_address)
    • Several methods and functions that return containers in ipaddr now return iterators. This includes subnets, address_exclude, summaryize_address_range, and collapse_address_list.

Please see PEP-3144 for more information .

+2


a source







All Articles