ValueError: '10 .0.0.0 / 24 'is not an IPv4 or IPv6 network

I want to work with IP subnets / IP addresses in Python. I have created Python code using the ipaddress module. When I run the code in the pycharm IDE, it works fine. But when I run the command line by typing python test.py , it shows the following error.

 ValueError: '10.0.0.0/24' does not appear to be an IPv4 or IPv6 network 

test.py

 import ipaddress srcIp = ipaddress.ip_network("10.0.0.0/24") print(srcIp) 
+7
source share
5 answers

It seems to work in Python 2.7 if you use a Unicode string.

 import ipaddress srcIp = ipaddress.ip_network(u'10.0.0.0/24') print srcIp 
+13
source

The main problem is that ip_network() creates an instance of the IPv4Network/IPv6Network , which requires the network address to be a unicode string. In Python 3, this is fine, but in Python 2, strings are not unicode by default. In 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 verbose message:

 >>> 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 this seems like a problem with unicode. One possible explanation is that perhaps PyCharm uses Python> = 3.3, which provides the ipaddress module in the standard library and in which the default strings are unicode. Your Python command line may have version 2 in which the default lines for byte lines and ipaddress.ip_network() will not work as shown above. I'm not sure about this because the print srcIp indicates that you are using Python 2 in both cases?

Another possibility is that PyCharm somehow affects the encoding of string literals in Python 2. I know almost nothing about PyCharm, but there are encoding options that can be set. Perhaps they really do something similar from __future__ import unicode_literals .

+7
source

Just noting that 10.0.0.0/24 is an invalid subnet. The first valid subnet on the network is 10.0.0.0/8 (class A), now 10.0.1.0/24 /24 subnet mask is ... 10.0.1.0/24 . You should throw up / down on the network side the same way you do for up / down on the host side of this bitmask. For the same reason, 10.255.255.0/24 is also not valid.

For any given subnet mask, there are 2 x - 2 subnets and 2 x - 2 hosts.

... where x is the number of bits on this side of the mask. Thus, for /24 it is 24 on the network side and 8 on the host side, which is 16777214 subnets and 254 hosts. Note the "- 2" part of this calculation on the network side of the bitmask. This means that you must throw them away (you cannot give them away), since in this case they mean something for the tcp / ip transport layer.

This should make sense to anyone who already knows that you also cannot link any 10.xy0/24 and 10.xy255/24 since they already mean something.

0
source

Rolling back the laptop version works for me.

 pip uninstall notebook pip install notebook==5.6.0 
0
source

Python 3:

 import ipaddress srcIp = ipaddress.ip_network(str('10.0.0.0/24')) 
0
source

All Articles