Skip to content

Commit 7f6d0ef

Browse files
committed
Use ipaddress on Python 2 to match maxminddb
1 parent 594caaa commit 7f6d0ef

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

geoip2/compat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Intended for internal use only."""
2+
import sys
3+
4+
import ipaddress
5+
6+
# pylint: skip-file
7+
8+
if sys.version_info[0] == 2:
9+
def compat_ip_address(address):
10+
"""Intended for internal use only."""
11+
if isinstance(address, bytes):
12+
address = address.decode()
13+
return ipaddress.ip_address(address)
14+
else:
15+
def compat_ip_address(address):
16+
"""Intended for internal use only."""
17+
return ipaddress.ip_address(address)

geoip2/webservice.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,19 @@
2525
2626
"""
2727

28-
import sys
29-
3028
import requests
3129

30+
from requests.utils import default_user_agent
31+
3232
import geoip2
3333
import geoip2.models
3434

35-
from requests.utils import default_user_agent
35+
from .compat import compat_ip_address
3636

3737
from .errors import (AddressNotFoundError, AuthenticationError, GeoIP2Error,
3838
HTTPError, InvalidRequestError, OutOfQueriesError,
3939
PermissionRequiredError)
4040

41-
if sys.version_info[0] == 2 or (sys.version_info[0] == 3 and
42-
sys.version_info[1] < 3):
43-
import ipaddr as ipaddress # pylint:disable=F0401
44-
45-
ipaddress.ip_address = ipaddress.IPAddress
46-
else:
47-
import ipaddress # pylint:disable=F0401
48-
4941

5042
class Client(object):
5143
"""Creates a new client object.
@@ -96,6 +88,7 @@ def __init__(self,
9688
host='geoip.maxmind.com',
9789
locales=None,
9890
timeout=None):
91+
"""Construct a Client."""
9992
# pylint: disable=too-many-arguments
10093
if locales is None:
10194
locales = ['en']
@@ -144,7 +137,7 @@ def insights(self, ip_address='me'):
144137

145138
def _response_for(self, path, model_class, ip_address):
146139
if ip_address != 'me':
147-
ip_address = str(ipaddress.ip_address(ip_address))
140+
ip_address = str(compat_ip_address(ip_address))
148141
uri = '/'.join([self._base_uri, path, ip_address])
149142
response = requests.get(uri,
150143
auth=(self._user_id, self._license_key),

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
include_package_data=True,
3434
install_requires=requirements,
3535
extras_require={
36-
':python_version=="2.6" or python_version=="2.7"': ['ipaddr']},
36+
':python_version=="2.6" or python_version=="2.7"': ['ipaddress']},
3737
tests_require=['requests_mock'],
3838
test_suite="tests",
3939
license=geoip2.__license__,

0 commit comments

Comments
 (0)