Edamame
Edamame

Reputation: 25406

python 2.7: AddressValueError: ip does not appear to be an IPv4 or IPv6 network

I am just running the example on: https://docs.python.org/3/howto/ipaddress.html#ipaddress-howto

However, in the following example:

import ipaddress

net4 = ipaddress.ip_network("192.0.2.0/24")
for x in net4.hosts():
    print(x)

I got the following error:

AddressValueErrorTraceback (most recent call last)
<ipython-input-18-256ed42a96d9> in <module>()
      1 import ipaddress
      2 
----> 3 net4 = ipaddress.ip_network("192.0.2.0/24")
      4 for x in net4.hosts():
      5     print(x)

/usr/local/lib/python2.7/dist-packages/ipaddress.pyc in ip_network(address, strict)
    197             '%r does not appear to be an IPv4 or IPv6 network. '
    198             'Did you pass in a bytes (str in Python 2) instead of'
--> 199             ' a unicode object?' % address)
    200 
    201     raise ValueError('%r does not appear to be an IPv4 or IPv6 network' %

AddressValueError: '192.0.2.0/24' does not appear to be an IPv4 or IPv6 network. Did you pass in a bytes (str in Python 2) instead of a unicode object?

Did I miss anything here (I am using python 2.7)? Thanks!

Upvotes: 5

Views: 22815

Answers (2)

SoSen
SoSen

Reputation: 339

You can change second line to

net4 = ipaddress.ip_network(unicode("192.0.2.0/24"))

The complete code looks like

import ipaddress

net4 = ipaddress.ip_network(unicode("192.0.2.0/24"))
for x in net4.hosts():
    print(x)

Upvotes: 5

Edamame
Edamame

Reputation: 25406

I resolved it by inputing a unicode string, similar to another question: ValueError: '10.0.0.0/24' does not appear to be an IPv4 or IPv6 network

Upvotes: 0

Related Questions