tags:

views:

316

answers:

1

Given either the binary or string representation of an IPv6 address and its prefix length, what's the best way to extract the prefix in Python?

Is there a library that would do this for me, or would I have to:

  1. convert the address from string to an int (inet_ntop)
  2. Mask out the prefix
  3. Convert prefix back to binary
  4. Convert binary to string (inet_ntop)
+1  A: 

See http://code.google.com/p/ipaddr-py/

With this, you can do

py> p=ipaddr.IPv6("2001:888:2000:d::a2")
py> p.SetPrefix(64)
py> p
IPv6('2001:888:2000:d::a2/64')
py> p.network_ext
'2001:888:2000:d::'

etc.

Martin v. Löwis