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

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

Closed
JohnnyZ opened this issue Jan 10, 2017 · 12 comments
Closed

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

JohnnyZ opened this issue Jan 10, 2017 · 12 comments

Comments

@JohnnyZ
Copy link

JohnnyZ commented Jan 10, 2017

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

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

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

@JohnnyZ
Copy link
Author

JohnnyZ commented Jan 11, 2017

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

What does the following output?

import ipaddress

print type(ipaddress.ip_address(u'128.101.101.101').packed)

@JohnnyZ
Copy link
Author

JohnnyZ commented Jan 11, 2017

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

@oschwald
Copy link
Member

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

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

JohnnyZ commented Jan 11, 2017

That worked! Thank you for your help

@JohnnyZ JohnnyZ closed this as completed Jan 11, 2017
@phihag
Copy link

phihag commented Jan 11, 2017

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

JohnnyZ commented Jan 11, 2017

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

phihag commented Jan 11, 2017

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

JohnnyZ commented Jan 11, 2017

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

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

No branches or pull requests

3 participants