Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,36 @@ If the database exists in the default path, the download will be skipped, but ca

### Available Methods

- getDetails(ip)
- getCountryDetails(ip)
- getCountry(ip)
- getCountryName(ip)
- getContinent(ip)
- getContinentName(ip)
- getASNDetails(ip)
- getASN(ip)
- getASNName(ip)
- getASNDomain(ip)
- close()

### Using the MMDB reader separately

Advanced users can use the MMDB reader that is included in the installation.

#### Sample Usage
```python
>>> from ipinfo_db.reader import Reader
>>> db = Reader('PATH_TO_MMDB_FILE')
>>> result = db.get('IP')
>>> result
```
#### Available Methods

- open(path)
- close()
- metadata()
- get(ip)
- getWithPrefixLen(ip)

## Other Libraries

Expand Down
11 changes: 8 additions & 3 deletions ipinfo_db/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import maxminddb
import urllib.request
import os
import appdirs
from ipinfo_db.reader import Reader

DB_DOWNLOAD_URL = "https://ipinfo.io/data/free/country_asn.mmdb?token="
DEFAULT_APP_PATH = appdirs.user_data_dir(appname='ipinfo_db', appauthor='ipinfo')
Expand Down Expand Up @@ -43,7 +43,12 @@ def __init__(self, access_token=None, path=None, replace=False):
urllib.request.urlretrieve(DB_DOWNLOAD_URL+self.access_token, self.path)

# Read the mmdb file.
self.db = maxminddb.open_database(self.path)
self.db = Reader(self.path)

def close(self):
Comment thread
abu-usama marked this conversation as resolved.
'''Closes the mmdb file.
'''
self.db.close()

def getDetails(self, ip):
'''Returns all the country and ASN level IP information available for the input IP address in a dictionary format.
Expand Down Expand Up @@ -145,4 +150,4 @@ def _get_data_field(self, ip, field):

def _get_data_dictionary(self, ip, fields):
data = self.db.get(ip)
return {k:data[key] for key in fields if key in data}
return {key:data[key] for key in fields if key in data}
Comment thread
abu-usama marked this conversation as resolved.
49 changes: 49 additions & 0 deletions ipinfo_db/reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import maxminddb

class Reader:

def __init__(self, path):
'''Initializes the Reader object with the given path.

:param: path: Path to the mmdb file.
'''
self.db = maxminddb.open_database(path)

def open(self, path):
'''Opens an mmdb file located at the given path. Closes previously opened database.

:param: path: Path to the mmdb file.
'''
self.close()
self.db = maxminddb.open_database(path)

def close(self):
'''Closes the database.
'''
self.db.close()

def metadata(self):
'''Returns the metadata associated with the mmdb file.

:return: metadata of the mmdb file.
:rtype: Metadata
'''
return self.db.metadata()

def get(self, ip):
'''Returns the database record for the given IP address.

:param: ip: An IP address in string format. Can be either IPv4 or IPv6.
:return: Database record for the given IP.
:rtype: Record
'''
return self.db.get(ip)

def getWithPrefixLen(self, ip):
'''Returns a tuple containing the database record and the associated (network) prefix length.

:param: ip: An IP address in string format. Can be either IPv4 or IPv6.
:return: A tuple containing the database record and the prefix length.
:rtype: Tuple
'''
return self.db.get_with_prefix_len(ip)
2 changes: 1 addition & 1 deletion ipinfo_db/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SDK_VERSION = "0.0.2"
SDK_VERSION = "0.0.3"
18 changes: 9 additions & 9 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@


def test_get_country():
assert(client.getCountry("8.8.8.8"), "US")
assert client.getCountry("8.8.8.8") == "US"

def test_get_country_name():
assert(client.getCountryName('8.8.8.8'),"United States")
assert client.getCountryName('8.8.8.8') == "United States"

def test_get_continent():
assert(client.getContinent('8.8.8.8'),"NA")
assert client.getContinent('8.8.8.8') == "NA"

def test_get_continent_name():
assert(client.getContinentName('8.8.8.8'),"North America")
assert client.getContinentName('8.8.8.8') == "North America"

def test_get_asn():
assert(client.getASN('8.8.8.8'),"AS15169")
assert client.getASN('8.8.8.8') == "AS15169"

def test_get_asn_name():
assert(client.getASNName('8.8.8.8'),"Google LLC")
assert client.getASNName('8.8.8.8') == "Google LLC"

def test_get_asn_domain():
assert(client.getASNDomain('8.8.8.8'),"google.com")
assert client.getASNDomain('8.8.8.8') == "google.com"

def test_no_ip_details():
assert(client.getDetails('127.0.0.1'), None)
assert(client.getCountry('127.0.0.1'), None)
assert client.getDetails('127.0.0.1') is None
assert client.getCountry('127.0.0.1') is None