简体   繁体   中英

IPv6 Address Python

I have a IPv6 address equal to "2001:200:e000::/35". However, I can't input it on function ip_address() from library ipaddress. The function works perfectly for addresses that don't contain "/", such as IPv4 "1.0.0.0" and IPv6 "2001:12f8:0:17::23":

ipaddress.ip_address(unicode("1.0.0.0","utf-8"))

returns:

IPv4Address(u'1.0.0.0')

And

ipaddress.ip_address(unicode("2001:12f8:0:17::23","utf-8"))

returns:

IPv6Address(u'2001:12f8:0:17::23')

However, when I try it for IPv6 "2001:200:e000::/35",

ipaddress.ip_address(unicode("2001:200:e000::/35","utf-8"))

returns:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/ipaddress.py", line 168, in ip_address
    address)
ValueError: u'2001:200:e000::/35' does not appear to be an IPv4 or IPv6 address

How am I supposed to input the IP address that contains "/" to ip_address?

"IP addresses with / in them" as you call them are not actually IP addresses. They are IP networks (which means a range of IP addresses) in something called "CIDR Notation".

You can process IP networks using ipaddress.ip_network() . For example:

>>> ipn = ipaddress.ip_network("2001:200:e000::/35")
>>> ipn.num_addresses
9903520314283042199192993792

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM