【问题标题】:python 2.7: AddressValueError: ip does not appear to be an IPv4 or IPv6 networkpython 2.7:AddressValueError:ip 似乎不是 IPv4 或 IPv6 网络
【发布时间】:2016-11-23 23:58:23
【问题描述】:

我只是在以下位置运行示例: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)?谢谢!

【问题讨论】:

    标签: python-2.7 ip


    【解决方案1】:

    您可以将第二行更改为

    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)
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2015-08-04
        • 1970-01-01
        • 2021-04-16
        • 1970-01-01
        • 1970-01-01
        • 2010-10-08
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        相关资源
        最近更新 更多