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 .
source share