python 2.7:AddressValueError:ip似乎不是IPv4或IPv6网络

时间:2016-11-23 23:58:24

标签: python-2.7 ip

我只是在https://docs.python.org/3/howto/ipaddress.html#ipaddress-howto

上运行示例

但是,在以下示例中:

import ipaddress

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

我收到以下错误:

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?

我在这里错过了什么(我使用的是python 2.7)?谢谢!

2 个答案:

答案 0 :(得分:0)

我通过输入unicode字符串来解决它,类似于另一个问题: ValueError: '10.0.0.0/24' does not appear to be an IPv4 or IPv6 network

答案 1 :(得分:0)

您可以将第二行更改为

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

完整的代码如下

import ipaddress

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