---
title: "Geolocation with GeoIP"
version: 1.9
locale: id
source: https://docs.djangoproject.com/id/1.9/ref/contrib/gis/geoip/
canonical: https://djangodocs.dev/id/1.9/ref/contrib/gis/geoip/
---
# Geolocation with GeoIP

> **Deprecated since Django 1.9**
>
> Ditinggalkan sejak versi 1.9: This module is deprecated in favor of [django.contrib.gis.geoip2](/id/1.9/ref/contrib/gis/geoip2/), which supports IPv6 and the GeoLite2 database
> format.

The [`GeoIP`](#django.contrib.gis.geoip.GeoIP) object is a ctypes wrapper for the
[MaxMind GeoIP C API](https://www.maxmind.com/app/c). [^1]

In order to perform IP-based geolocation, the [`GeoIP`](#django.contrib.gis.geoip.GeoIP) object requires
the GeoIP C library and either the GeoIP [Country](https://www.maxmind.com/app/country) or [City](https://www.maxmind.com/app/city)
datasets in binary format (the CSV files will not work!).  These datasets may be
[downloaded from MaxMind](https://www.maxmind.com/download/geoip/database/).  Grab the `GeoLiteCountry/GeoIP.dat.gz` and
`GeoLiteCity.dat.gz` files and unzip them in a directory corresponding to what
you set [`GEOIP_PATH`](/id/1.9/ref/contrib/gis/geoip2/#std-setting-GEOIP_PATH) with in your settings.  See the example and
reference below for more details.

## Contoh

Assuming you have the GeoIP C library installed, here is an example of its
usage:

```
>>> from django.contrib.gis.geoip import GeoIP
>>> g = GeoIP()
>>> g.country('google.com')
{'country_code': 'US', 'country_name': 'United States'}
>>> g.city('72.14.207.99')
{'area_code': 650,
'city': 'Mountain View',
'country_code': 'US',
'country_code3': 'USA',
'country_name': 'United States',
'dma_code': 807,
'latitude': 37.419200897216797,
'longitude': -122.05740356445312,
'postal_code': '94043',
'region': 'CA'}
>>> g.lat_lon('salon.com')
(37.789798736572266, -122.39420318603516)
>>> g.lon_lat('uh.edu')
(-95.415199279785156, 29.77549934387207)
>>> g.geos('24.124.1.80').wkt
'POINT (-95.2087020874023438 39.0392990112304688)'
```

## Pengaturan `GeoIP`

### `GEOIP_PATH`

A string specifying the directory where the GeoIP data files are
located.  This setting is *required* unless manually specified
with `path` keyword when initializing the [`GeoIP`](#django.contrib.gis.geoip.GeoIP) object.

### `GEOIP_LIBRARY_PATH`

A string specifying the location of the GeoIP C library.  Typically,
this setting is only used if the GeoIP C library is in a non-standard
location (e.g., `/home/sue/lib/libGeoIP.so`).

### `GEOIP_COUNTRY`

The basename to use for the GeoIP country data file.
Defaults to `'GeoIP.dat'`.

### `GEOIP_CITY`

The basename to use for the GeoIP city data file.
Defaults to `'GeoLiteCity.dat'`.

## API `GeoIP`

#### `class GeoIP(path=None, cache=0, country=None, city=None)`

The `GeoIP` object does not require any parameters to use the default
settings.  However, at the very least the [`GEOIP_PATH`](/id/1.9/ref/contrib/gis/geoip2/#std-setting-GEOIP_PATH) setting
should be set with the path of the location of your GeoIP data sets.  The
following initialization keywords may be used to customize any of the
defaults.

| Keyword Arguments | Deskripsi |
| --- | --- |
| `path` | Base directory to where GeoIP data is located or the<br>full path to where the city or country data files<br>(.dat) are located.  Assumes that both the city and<br>country data sets are located in this directory;<br>overrides the [`GEOIP_PATH`](/id/1.9/ref/contrib/gis/geoip2/#std-setting-GEOIP_PATH) settings attribute. |
| `cache` | The cache settings when opening up the GeoIP datasets,<br>and may be an integer in (0, 1, 2, 4) corresponding to<br>the `GEOIP_STANDARD`, `GEOIP_MEMORY_CACHE`,<br>`GEOIP_CHECK_CACHE`, and `GEOIP_INDEX_CACHE`<br>`GeoIPOptions` C API settings, respectively.<br>Defaults to 0 (`GEOIP_STANDARD`). |
| `country` | The name of the GeoIP country data file.  Defaults<br>to `GeoIP.dat`.  Setting this keyword overrides the<br>[`GEOIP_COUNTRY`](/id/1.9/ref/contrib/gis/geoip2/#std-setting-GEOIP_COUNTRY) settings attribute. |
| `city` | The name of the GeoIP city data file.  Defaults to<br>`GeoLiteCity.dat`.  Setting this keyword overrides<br>the [`GEOIP_CITY`](/id/1.9/ref/contrib/gis/geoip2/#std-setting-GEOIP_CITY) settings attribute. |

## Cara `GeoIP`

### Querying

All the following querying routines may take either a string IP address
or a fully qualified domain name (FQDN).  For example, both
`'205.186.163.125'` and `'djangoproject.com'` would be valid query
parameters.

#### `GeoIP.city(query)`

Returns a dictionary of city information for the given query.  Some
of the values in the dictionary may be undefined (`None`).

#### `GeoIP.country(query)`

Returns a dictionary with the country code and country for the given
query.

#### `GeoIP.country_code(query)`

Returns only the country code corresponding to the query.

#### `GeoIP.country_name(query)`

Returns only the country name corresponding to the query.

### Pengambilan Kordinat

#### `GeoIP.coords(query)`

Returns a coordinate tuple of (longitude, latitude).

#### `GeoIP.lon_lat(query)`

Returns a coordinate tuple of (longitude, latitude).

#### `GeoIP.lat_lon(query)`

Returns a coordinate tuple of (latitude, longitude),

#### `GeoIP.geos(query)`

Returns a [`django.contrib.gis.geos.Point`](/id/1.9/ref/contrib/gis/geos/#django.contrib.gis.geos.Point) object corresponding to the query.

### Informasi Basisdata

#### `GeoIP.country_info`

This property returns information about the GeoIP country database.

#### `GeoIP.city_info`

This property returns information about the GeoIP city database.

#### `GeoIP.info`

This property returns information about all GeoIP databases (both city
and country), and the version of the GeoIP C library (if supported).

### GeoIP-Python API compatibility methods

These methods exist to ease compatibility with any code using MaxMind's
existing Python API.

#### `classmethod GeoIP.open(path, cache)`

This classmethod instantiates the GeoIP object from the given database path
and given cache setting.

#### `GeoIP.region_by_addr(query)`

#### `GeoIP.region_by_name(query)`

#### `GeoIP.record_by_addr(query)`

#### `GeoIP.record_by_name(query)`

#### `GeoIP.country_code_by_addr(query)`

#### `GeoIP.country_code_by_name(query)`

#### `GeoIP.country_name_by_addr(query)`

#### `GeoIP.country_name_by_name(query)`

**Catatan kaki**

[^1]: GeoIP(R) adalah merek dagang terdaftar dari MaxMind, LLC dari Boston, Massachusetts.
