[英]ValueError: '10.0.0.0/24' does not appear to be an IPv4 or IPv6 network
I want to work with IP subnets / IP addresses in Python. 我想在Python中使用IP子网/ IP地址。 I created the Python code using the
ipaddress
module. 我使用
ipaddress
模块创建了Python代码。 When I run the code in the pycharm IDE, it works fine. 当我在pycharm IDE中运行代码时,它工作正常。 But when I run on the command prompt by typing
python test.py
, it shows the following error. 但是当我通过键入
python test.py
在命令提示符下运行时,它会显示以下错误。
ValueError: '10.0.0.0/24' does not appear to be an IPv4 or IPv6 network
test.py : test.py :
import ipaddress
srcIp = ipaddress.ip_network("10.0.0.0/24")
print(srcIp)
It seems to work in Python 2.7, if you use a Unicode string. 如果你使用Unicode字符串,它似乎在Python 2.7中工作。
import ipaddress
srcIp = ipaddress.ip_network(u'10.0.0.0/24')
print srcIp
The underlying problem is that ip_network()
instantiates a IPv4Network/IPv6Network
object which requires the network address to be a unicode string. 根本问题是
ip_network()
实例化IPv4Network/IPv6Network
对象,该对象要求网络地址为unicode字符串。 In Python 3 this is fine, but in Python 2 strings are not unicode by default. 在Python 3中这很好,但在Python 2中,默认情况下字符串不是unicode。 In Python 2:
在Python 2中:
>>> import ipaddress
>>> ipaddress.IPv4Network('10.0.0.0/24')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ipaddress.py", line 1486, in __init__
self.network_address = IPv4Address(address)
File "ipaddress.py", line 1271, in __init__
self._check_packed_address(address, 4)
File "ipaddress.py", line 528, in _check_packed_address
expected_len, self._version))
ipaddress.AddressValueError: '10.0.0.0/24' (len 11 != 4) is not permitted as an IPv4 address (did you pass in a bytes instead of a unicode object?)
>>> ipaddress.IPv4Network(u'10.0.0.0/24')
IPv4Network(u'10.0.0.0/24')
ipaddress.ip_network()
catches this exception and raises a ValueError
with a less detailed message: ipaddress.ip_network()
捕获此异常并使用不太详细的消息引发ValueError
:
>>> ipaddress.ip_network('10.0.0.0/24')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ipaddress.py", line 148, in ip_network
address)
ValueError: '10.0.0.0/24' does not appear to be an IPv4 or IPv6 network
So it looks like a unicode issue. 所以它看起来像一个unicode问题。 One possible explanation is that maybe PyCharm is using Python >= 3.3 which provides module
ipaddress
in the standard library and in which strings are unicode by default. 一种可能的解释是,PyCharm可能正在使用Python> = 3.3,它在标准库中提供模块
ipaddress
,默认情况下字符串是unicode。 Your command line Python could be version 2, in which strings default to byte strings, and ipaddress.ip_network()
will fail as shown above. 您的命令行Python可能是版本2,其中字符串默认为字节字符串,而
ipaddress.ip_network()
将失败,如上所示。 I'm not sure about this because the print srcIp
statement indicates that you are using Python 2 in both cases? 我不确定这一点,因为
print srcIp
语句表明你在两种情况下都使用Python 2?
Another possibility is that PyCharm is somehow affecting the encoding of string literals within Python 2. I know almost nothing about PyCharm, but there are encoding options that can be set. 另一种可能性是PyCharm在某种程度上影响了Python 2中字符串文字的编码。我对PyCharm几乎一无所知,但是可以设置编码选项。 Perhaps these effectively do something similar to
from __future__ import unicode_literals
. 也许这些有效地
from __future__ import unicode_literals
做了类似的from __future__ import unicode_literals
。
Just noting that 10.0.0.0/24 is an invalid subnet. 注意10.0.0.0/24是一个无效的子网。 The first valid subnet within the 10.0.0.0/8 (Class A) network, now sliced with a
/24
subnet mask is... 10.0.1.0/24
. 10.0.0.0/8(A类)网络中的第一个有效子网,现在用
/24
子网掩码切片为... 10.0.1.0/24
。 You have to throw away the top/bottom on the network side just like you do for the top/bottom for the host side of that bitmask. 您必须丢弃网络端的顶部/底部,就像您对该位掩码的主机端的顶部/底部一样。 For the same reason, 10.255.255.0/24 is also invalid.
出于同样的原因,10.255.255.0 / 24也无效。
For any given subnet mask there are 2 x - 2 subnets and 2 x - 2 hosts 对于任何给定的子网掩码,有2 x - 2个子网和2 x - 2个主机
...where x is the number of bits on that side of the mask. ...其中x是掩码那一侧的位数。 So for
/24
that's 24 on the network side and 8 on the host side making 16777214 subnets and 254 hosts. 因此对于
/24
,网络端为/24
,主机端为8,制作16777214个子网和254个主机。 Note the "- 2" part of that calculation on the network side of the bitmask. 注意位掩码的网络侧的计算的“ - 2”部分。 That means that you have to throw away (you can't issue) those since they mean something to the transport layer of tcp/ip, in this case.
这意味着你必须扔掉(你不能发出)那些因为它们对tcp / ip的传输层有意义,在这种情况下。
This should make sense to anyone who already knows that you similarly can't bind any 10.xy0/24
and 10.xy255/24
addresses since they already mean something. 对于那些已经知道你同样不能绑定任何
10.xy0/24
和10.xy255/24
地址的人来说,这应该是有意义的,因为它们已经意味着什么。
Rolling back the notebook version works for me. 回滚笔记本版本对我有用。
pip uninstall notebook
pip install notebook==5.6.0
Python 3: Python 3:
import ipaddress
srcIp = ipaddress.ip_network(str('10.0.0.0/24'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.