Skip to content

Commit f7da388

Browse files
committed
Validate IP addresses with the ipaddress module (ipaddr for Python 2.x)
1 parent 72c1a00 commit f7da388

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
language: python
2+
23
python:
34
- 2.6
45
- 2.7
56
- 3.2
67
- 3.3
78
- pypy
9+
810
install:
911
- pip install -r requirements.txt
1012
- pip install git+https://github.com/maxmind/HTTPretty.git
1113
- pip install pylint coveralls
12-
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi
14+
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2 ipaddr; fi
15+
- if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then pip install ipaddr; fi
16+
1317
script:
1418
- coverage run --source=geoip2 setup.py test
1519
- if [[ $TRAVIS_PYTHON_VERSION != '3.3' ]]; then pylint geoip2; fi
20+
1621
after_success:
1722
- coveralls
23+
1824
notifications:
1925
email:
2026
recipients:

geoip2/webservices.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@
100100
from requests.utils import default_user_agent
101101
from .errors import GeoIP2Error, HTTPError, WebServiceError
102102

103+
import sys
104+
105+
if sys.version_info[0] == 2:
106+
import ipaddr as ipaddress
107+
ipaddress.ip_address = ipaddress.IPAddress
108+
else:
109+
import ipaddress
103110

104111
class Client(object):
105112
"""This method creates a new client object.
@@ -204,6 +211,8 @@ def omni(self, ip_address='me'):
204211
return self._response_for('omni', geoip2.models.Omni, ip_address)
205212

206213
def _response_for(self, path, model_class, ip_address):
214+
if ip_address != 'me':
215+
ip_address = str(ipaddress.ip_address(ip_address))
207216
uri = '/'.join([self._base_uri, path, ip_address])
208217
response = requests.get(uri, auth=(self.user_id, self.license_key),
209218
headers={'Accept': 'application/json',

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
requests
1+
requests>=1.0

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
packages = ['geoip2']
1818

19+
requirements = [i.strip() for i in open("requirements.txt").readlines()]
20+
21+
if sys.version_info[0] == 2:
22+
requirements.append('ipaddr')
1923

2024
setup(
2125
name='geoip2',
@@ -30,7 +34,7 @@
3034
package_data={'': ['LICENSE'] },
3135
package_dir={'geoip2': 'geoip2'},
3236
include_package_data=True,
33-
install_requires=['requests'],
37+
install_requires=requirements,
3438
test_suite="tests",
3539
license=open('LICENSE').read(),
3640
classifiers=(

tests/webservices_test.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,10 @@ def test_200_error(self):
104104
self.client.country('1.1.1.1')
105105

106106
def test_400_error(self):
107-
body = {'code': 'IP_ADDRESS_INVALID',
108-
'error': 'The value "1.2.3" is not a '
109-
'valid ip address',}
110-
httpretty.register_uri(httpretty.GET,
111-
self.base_uri + 'country/1.2.3',
112-
body = json.dumps(body),
113-
status = 400,
114-
content_type = self._content_type('country'))
115-
with self.assertRaisesRegex(WebServiceError,
116-
'The value "1.2.3" is not a valid '
117-
'ip address'):
118-
self.client.country('1.2.3')
119-
# XXX - there might be a better way to do this
120-
try:
107+
with self.assertRaisesRegex(ValueError,
108+
"'1.2.3' does not appear to be an IPv4 "
109+
"or IPv6 address"):
121110
self.client.country('1.2.3')
122-
except WebServiceError as e:
123-
self.assertEqual(e.http_status, 400,
124-
'exception object contains expected http_status'
125-
)
126-
self.assertEqual(e.code,'IP_ADDRESS_INVALID',
127-
'exception object contains expected code'
128-
)
129111

130112
def test_no_body_error(self):
131113
httpretty.register_uri(httpretty.GET,

0 commit comments

Comments
 (0)