maxmind / GeoIP2-python

Python code for GeoIP2 webservice client and database reader
https://geoip2.readthedocs.org/en/latest/
Apache License 2.0
1.11k stars 141 forks source link

TypeError: ord() expected string of length 1, but int found #41

Closed JohnnyZ closed 7 years ago

JohnnyZ commented 7 years ago

Hey, not sure exactly what is happening here. Thank you in advance.

MAC 10.12.2

import geoip2 import geoip2.database reader = geoip2.database.Reader('GeoLite2-City.mmdb') response = reader.city('128.101.101.101') TypeError Traceback (most recent call last)

in () 2 import geoip2.database 3 reader = geoip2.database.Reader('GeoLite2-City.mmdb') ----> 4 response = reader.city('128.101.101.101') /Users/username/.pyenv/versions/venv27/lib/python2.7/site-packages/geoip2/database.pyc in city(self, ip_address) 108 109 """ --> 110 return self._model_for(geoip2.models.City, 'City', ip_address) 111 112 def anonymous_ip(self, ip_address): /Users/username/.pyenv/versions/venv27/lib/python2.7/site-packages/geoip2/database.pyc in _model_for(self, model_class, types, ip_address) 178 179 def _model_for(self, model_class, types, ip_address): --> 180 record = self._get(types, ip_address) 181 record.setdefault('traits', {})['ip_address'] = ip_address 182 return model_class(record, locales=self._locales) /Users/username/.pyenv/versions/venv27/lib/python2.7/site-packages/geoip2/database.pyc in _get(self, database_type, ip_address) 171 "%s database" % 172 (caller, self.metadata().database_type)) --> 173 record = self._db_reader.get(ip_address) 174 if record is None: 175 raise geoip2.errors.AddressNotFoundError( /Users/username/.pyenv/versions/venv27/lib/python2.7/site-packages/maxminddb/reader.pyc in get(self, ip_address) 102 'an IPv6 address in an IPv4-only database.'.format( 103 ip_address)) --> 104 pointer = self._find_address_in_tree(address) 105 106 return self._resolve_data_pointer(pointer) if pointer else None /Users/username/.pyenv/versions/venv27/lib/python2.7/site-packages/maxminddb/reader.pyc in _find_address_in_tree(self, ip_address) 115 if node >= self._metadata.node_count: 116 break --> 117 bit = 1 & (int_from_byte(packed[i >> 3]) >> 7 - (i % 8)) 118 node = self._read_node(node, bit) 119 if node == self._metadata.node_count: TypeError: ord() expected string of length 1, but int found
oschwald commented 7 years ago

Are you using the latest version of maxminddb and the latest GeoLite2 City database? Would you provide the output of md5sum GeoLite2-City.mmdb? Thanks!

oschwald commented 7 years ago

Also, can you make sure you have the latest version of ipaddress? The incorrect value appears to be coming from it.

JohnnyZ commented 7 years ago

Just upgraded my from x.x.16 to ipaddress==1.0.17 and current version of maxminddb==1.2.2.

Same error exists

And the md5sum 5f9f088f7dcf8b4ba3895b6b68930590 GeoLite2-City.mmdb . I downloaded it fresh from the site today. Let me know if it is outdated.

Thank you for your help!

oschwald commented 7 years ago

What does the following output?

import ipaddress

print type(ipaddress.ip_address(u'128.101.101.101').packed)
JohnnyZ commented 7 years ago

print type(ipaddress.ip_address(u'128.101.101.101').packed) <type 'bytearray'>

oschwald commented 7 years ago

Thanks. That significantly narrows down the issue. The type of the returned value is str for me on 2.7.13 running on Ubuntu.

oschwald commented 7 years ago

Would it be possible for you to test the branch greg/packed-bytearray-fix for maxminddb? I think this should work with both return types.

JohnnyZ commented 7 years ago

That worked! Thank you for your help

phihag commented 7 years ago

@johnnyz What output do you get for

import ipaddress
print(ipaddress.__file__)

You may have installed py2-ipaddress by accident. That package claims the name ipaddress name as well, despite having an incompatible API to the Python 3 (and thus, my backport). Their rationale is that they never want to update to Python 3, so they don't care about compability. I have adapted an email of mine about why py2-ipaddress is dangerous.

JohnnyZ commented 7 years ago

Here you go

print(ipaddress.file) /Users/username/.pyenv/versions/venv27/lib/python2.7/site-packages/backport_ipaddress-0.1-py2.7.egg/ipaddress.py

phihag commented 7 years ago

Yup, that's backport_ipaddress, a fork of py2-ipaddress which shares the same concerns (and some more - the latest update was 2014!). Uninstall the package backport_ipaddress and everything should work.

JohnnyZ commented 7 years ago

Thank you for your help and informing me of the concerning package.