Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pa4080 opened this issue Nov 5, 2021 · 3 comments

Comments

@pa4080
Copy link

pa4080 commented Nov 5, 2021

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
Copy link

AlastairBateman commented Dec 14, 2021

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
Copy link

kesawi commented Jan 27, 2022

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

@bobzilladev
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants