sherpya / geolite2legacy

MaxMind GeoLite2 (CSV) to Legacy format converter
MIT License
254 stars 88 forks source link

ipaddress.AddressValueError: '1.0.0.0/24' does not appear to be an IPv4 or IPv6 network. #39

Open pa4080 opened 2 years ago

pa4080 commented 2 years ago

I'm on fresh Ubuntu Server 20.04. The python-ipaddress is installed by the command:

sudo apt-get install -y python-ipaddress

But geolite2legacy.py returns the following error:

./geolite2legacy.py -i GeoLite2-Country-CSV.zip -f geoname2fips.csv -o GeoIP.dat
Database type Country - Blocks IPv4 - Encoding: utf-8
Traceback (most recent call last):
  File "./geolite2legacy.py", line 475, in <module>
    main()
  File "./geolite2legacy.py", line 459, in main
    r.load(locs, TextIOWrapper(ziparchive.open(blocks, 'r'), encoding='utf-8'))
  File "./geolite2legacy.py", line 162, in load
    for nets, data in self.gen_nets(locations, outfile):
  File "./geolite2legacy.py", line 319, in gen_nets
    nets = [IPNetwork(row['network'])]
  File "/usr/lib/python2.7/dist-packages/ipaddress.py", line 199, in ip_network
    ' a unicode object?' % address)
ipaddress.AddressValueError: '1.0.0.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?

In order to solve this error I've made the following change:

git diff
diff --git a/geolite2legacy.py b/geolite2legacy.py
index e6a39f1..73c3780 100755
--- a/geolite2legacy.py
+++ b/geolite2legacy.py
@@ -225,7 +225,7 @@ class ASNRadixTree(RadixTree):

     def gen_nets(self, locations, infile):
         for row in csv.DictReader(infile):
-            nets = [IPNetwork(row['network'])]
+            nets = [IPNetwork(row['network'].decode('utf-8'))]
             org = decode_text(row['autonomous_system_organization'])
             asn = row['autonomous_system_number']
             entry = u'AS{} {}'.format(asn, org)
@@ -254,7 +254,7 @@ class CityRev1RadixTree(RadixTree):
             if location is None:
                 continue

-            nets = [IPNetwork(row['network'])]
+            nets = [IPNetwork(row['network'].decode('utf-8'))]
             country_iso_code = location['country_iso_code'] or location['continent_code']
             fips_code = geoname2fips.get(location['geoname_id'])
             if fips_code is None:
@@ -316,7 +316,7 @@ class CountryRadixTree(RadixTree):
             if location is None:
                 continue

-            nets = [IPNetwork(row['network'])]
+            nets = [IPNetwork(row['network'].decode('utf-8'))]
             country_iso_code = location['country_iso_code'] or location['continent_code']
             yield nets, (country_iso_code,)

Does this a correct way to solve that issue?

AlastairBateman commented 2 years ago

I made a similar change but:

   nets = [IPNetwork(unicode(row['network']))]

Disclaimer: I'm not a python dev, so this isn't guaranteed to be a foolproof solution.

kesawi commented 2 years ago

Thanks, this fixed the issue I was having on CentOS 7

bobzilladev commented 1 year ago

fyi: ran into this on python 2.7, goes away on python3.