11
11
GeoIP_country_name_by_addr, GeoIP_country_name_by_name)
13
13
from django.utils import six
14
from django.utils.encoding import force_bytes
15
16
# Regular expressions for recognizing the GeoIP free database editions.
16
17
free_regex = re.compile(r'^GEO-\d{3}FREE')
99
100
# and/or city datasets exist, then try and open them.
100
101
country_db = os.path.join(path, country or GEOIP_SETTINGS.get('GEOIP_COUNTRY', 'GeoIP.dat'))
101
102
if os.path.isfile(country_db):
102
self._country = GeoIP_open(country_db, cache)
103
self._country = GeoIP_open(force_bytes(country_db), cache)
103
104
self._country_file = country_db
105
106
city_db = os.path.join(path, city or GEOIP_SETTINGS.get('GEOIP_CITY', 'GeoIPCity.dat'))
106
107
if os.path.isfile(city_db):
107
self._city = GeoIP_open(city_db, cache)
108
self._city = GeoIP_open(force_bytes(city_db), cache)
108
109
self._city_file = city_db
109
110
elif os.path.isfile(path):
110
111
# Otherwise, some detective work will be needed to figure
111
112
# out whether the given database path is for the GeoIP country
112
113
# or city databases.
113
ptr = GeoIP_open(path, cache)
114
ptr = GeoIP_open(force_bytes(path), cache)
114
115
info = GeoIP_database_info(ptr)
115
116
if lite_regex.match(info):
116
117
# GeoLite City database detected.
136
139
if not isinstance(query, six.string_types):
137
140
raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__)
139
# GeoIP only takes ASCII-encoded strings.
140
query = query.encode('ascii')
142
142
# Extra checks for the existence of country and city databases.
143
143
if city_or_country and not (self._country or self._city):
144
144
raise GeoIPException('Invalid GeoIP country and city data files.')
147
147
elif city and not self._city:
148
148
raise GeoIPException('Invalid GeoIP city data file: %s' % self._city_file)
150
# Return the query string back to the caller.
150
# Return the query string back to the caller. GeoIP only takes bytestrings.
151
return force_bytes(query)
153
153
def city(self, query):
156
156
Fully Qualified Domain Name (FQDN). Some information in the dictionary
157
157
may be undefined (None).
159
query = self._check_query(query, city=True)
159
enc_query = self._check_query(query, city=True)
160
160
if ipv4_re.match(query):
161
161
# If an IP address was passed in
162
return GeoIP_record_by_addr(self._city, c_char_p(query))
162
return GeoIP_record_by_addr(self._city, c_char_p(enc_query))
164
164
# If a FQDN was passed in.
165
return GeoIP_record_by_name(self._city, c_char_p(query))
165
return GeoIP_record_by_name(self._city, c_char_p(enc_query))
167
167
def country_code(self, query):
168
168
"Returns the country code for the given IP Address or FQDN."
169
query = self._check_query(query, city_or_country=True)
169
enc_query = self._check_query(query, city_or_country=True)
170
170
if self._country:
171
171
if ipv4_re.match(query):
172
return GeoIP_country_code_by_addr(self._country, query)
172
return GeoIP_country_code_by_addr(self._country, enc_query)
174
return GeoIP_country_code_by_name(self._country, query)
174
return GeoIP_country_code_by_name(self._country, enc_query)
176
176
return self.city(query)['country_code']
178
178
def country_name(self, query):
179
179
"Returns the country name for the given IP Address or FQDN."
180
query = self._check_query(query, city_or_country=True)
180
enc_query = self._check_query(query, city_or_country=True)
181
181
if self._country:
182
182
if ipv4_re.match(query):
183
return GeoIP_country_name_by_addr(self._country, query)
183
return GeoIP_country_name_by_addr(self._country, enc_query)
185
return GeoIP_country_name_by_name(self._country, query)
185
return GeoIP_country_name_by_name(self._country, enc_query)
187
187
return self.city(query)['country_name']