Skip to content

Commit 8e33280

Browse files
committed
add timeout for webservice
1 parent 4f24aba commit 8e33280

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

geoip2/webservice.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
2626
"""
2727

28+
import sys
29+
2830
import geoip2
2931
import geoip2.models
3032
import requests
@@ -33,18 +35,17 @@
3335
GeoIP2Error, HTTPError, InvalidRequestError,
3436
OutOfQueriesError)
3537

36-
import sys
3738

3839
if sys.version_info[0] == 2 or (sys.version_info[0] == 3
3940
and sys.version_info[1] < 3):
4041
import ipaddr as ipaddress # pylint:disable=F0401
42+
4143
ipaddress.ip_address = ipaddress.IPAddress
4244
else:
4345
import ipaddress # pylint:disable=F0401
4446

4547

4648
class Client(object):
47-
4849
"""Creates a new client object.
4950
5051
It accepts the following required arguments:
@@ -88,13 +89,14 @@ class Client(object):
8889
"""
8990

9091
def __init__(self, user_id, license_key, host='geoip.maxmind.com',
91-
locales=None):
92+
locales=None, timeout=None):
9293
if locales is None:
9394
locales = ['en']
9495
self._locales = locales
9596
self._user_id = user_id
9697
self._license_key = license_key
9798
self._base_uri = 'https://%s/geoip/v2.1' % host
99+
self._timeout = timeout
98100

99101
def city(self, ip_address='me'):
100102
"""This method calls the GeoIP2 Precision City endpoint.
@@ -140,7 +142,8 @@ def _response_for(self, path, model_class, ip_address):
140142
uri = '/'.join([self._base_uri, path, ip_address])
141143
response = requests.get(uri, auth=(self._user_id, self._license_key),
142144
headers={'Accept': 'application/json',
143-
'User-Agent': self._user_agent()})
145+
'User-Agent': self._user_agent()},
146+
timeout=self._timeout)
144147
if response.status_code == 200:
145148
body = self._handle_success(response, uri)
146149
return model_class(body, locales=self._locales)
@@ -214,6 +217,8 @@ def _handle_non_200_status(self, status, uri):
214217
raise HTTPError('Received a very surprising HTTP status '
215218
'(%(status)i) for %(uri)s' % locals(), status,
216219
uri)
220+
221+
217222
"""
218223
219224
:copyright: (c) 2014 by MaxMind, Inc.

0 commit comments

Comments
 (0)