Skip to content
Closed
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,20 @@ print(db.languages())
print(db.fields())
print(db.build_time())
print(db.find_map("117.136.83.55", "CN"))
</pre>
</pre>

### 支持加载压缩文件

除了直接加载 `ipdb` 格式文件外,还可以加载压缩文件

- 支持 `gzip`

<pre>
import ipdb
db = ipdb.BaseStation("/path/to/base_station.ipdb.gz", compression='gzip')
print(db.is_ipv4(), db.is_ipv6())
print(db.languages())
print(db.fields())
print(db.build_time())
print(db.find_map("117.136.83.55", "CN"))
</pre>
44 changes: 3 additions & 41 deletions ipdb/base_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:copyright: ©2018 by IPIP.net
"""

from .database import Reader
from .database import Database


class BaseStationInfo:
Expand All @@ -20,44 +20,6 @@ def __init__(self, **kwargs):
self.__dict__[key] = self._map[key]


class BaseStation:
class BaseStation(Database):

db = None

def __init__(self, name):
self.db = Reader(name)

def reload(self, name):
try:
db = Reader(name)
self.db = db
return True
except:
return False

def find(self, addr, language):
return self.db.find(addr, language)

def find_map(self, addr, language):
return self.db.find_map(addr, language)

def find_info(self, addr, language):
m = self.db.find_map(addr, language)
if m is None:
return None
return BaseStationInfo(**m)

def is_ipv4(self):
return self.db.is_support_ipv4()

def is_ipv6(self):
return self.db.is_support_ipv6()

def languages(self):
return self.db.support_languages()

def fields(self):
return self.db.support_fields()

def build_time(self):
return self.db.build_utc_time()
info = BaseStationInfo
44 changes: 3 additions & 41 deletions ipdb/city.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:copyright: ©2018 by IPIP.net
"""

from .database import Reader
from .database import Database


class CityInfo:
Expand Down Expand Up @@ -34,44 +34,6 @@ def __init__(self, **kwargs):
self.__dict__[key] = self._map[key]


class City:
class City(Database):

db = None

def __init__(self, name):
self.db = Reader(name)

def reload(self, name):
try:
db = Reader(name)
self.db = db
return True
except:
return False

def find(self, addr, language):
return self.db.find(addr, language)

def find_map(self, addr, language):
return self.db.find_map(addr, language)

def find_info(self, addr, language):
m = self.db.find_map(addr, language)
if m is None:
return None
return CityInfo(**m)

def is_ipv4(self):
return self.db.is_support_ipv4()

def is_ipv6(self):
return self.db.is_support_ipv6()

def languages(self):
return self.db.support_languages()

def fields(self):
return self.db.support_fields()

def build_time(self):
return self.db.build_utc_time()
info = CityInfo
50 changes: 47 additions & 3 deletions ipdb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys

from .util import bytes2long
from .util import read_file
from .exceptions import NoSupportIPv4Error, NoSupportIPv6Error, NoSupportLanguageError, DatabaseError, IPNotFound


Expand All @@ -31,9 +32,8 @@ class Reader:
_v4offset = 0
_v6offsetCache = {}

def __init__(self, name):
file = open(name, "rb")
self.data = file.read()
def __init__(self, name, compression=None):
self.data = read_file(name, compression=compression)
self._file_size = len(self.data)

meta_length = bytes2long(self.data[0], self.data[1], self.data[2], self.data[3])
Expand Down Expand Up @@ -162,3 +162,47 @@ def is_support_ipv6(self):

def build_utc_time(self):
return self._meta.build


class Database:

db = None
info = None

def __init__(self, name, compression=None):
self.db = Reader(name, compression=compression)

def reload(self, name, compression=None):
try:
db = Reader(name, compression=compression)
self.db = db
return True
except:
return False

def find(self, addr, language):
return self.db.find(addr, language)

def find_map(self, addr, language):
return self.db.find_map(addr, language)

def find_info(self, addr, language):
m = self.db.find_map(addr, language)
if m is None:
return None
return self.info(**m)

def is_ipv4(self):
return self.db.is_support_ipv4()

def is_ipv6(self):
return self.db.is_support_ipv6()

def languages(self):
return self.db.support_languages()

def fields(self):
return self.db.support_fields()

def build_time(self):
return self.db.build_utc_time()
44 changes: 3 additions & 41 deletions ipdb/district.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:copyright: ©2018 by IPIP.net
"""

from .database import Reader
from .database import Database


class DistrictInfo:
Expand All @@ -22,44 +22,6 @@ def __init__(self, **kwargs):
self.__dict__[key] = self._map[key]


class District:
class District(Database):

db = None

def __init__(self, name):
self.db = Reader(name)

def reload(self, name):
try:
db = Reader(name)
self.db = db
return True
except:
return False

def find(self, addr, language):
return self.db.find(addr, language)

def find_map(self, addr, language):
return self.db.find_map(addr, language)

def find_info(self, addr, language):
m = self.db.find_map(addr, language)
if m is None:
return None
return DistrictInfo(**m)

def is_ipv4(self):
return self.db.is_support_ipv4()

def is_ipv6(self):
return self.db.is_support_ipv6()

def languages(self):
return self.db.support_languages()

def fields(self):
return self.db.support_fields()

def build_time(self):
return self.db.build_utc_time()
info = DistrictInfo
44 changes: 3 additions & 41 deletions ipdb/idc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:copyright: ©2018 by IPIP.net
"""

from .database import Reader
from .database import Database


class IDCInfo:
Expand All @@ -20,44 +20,6 @@ def __init__(self, **kwargs):
self.__dict__[key] = self._map[key]


class IDC:
class IDC(Database):

db = None

def __init__(self, name):
self.db = Reader(name)

def reload(self, name):
try:
db = Reader(name)
self.db = db
return True
except:
return False

def find(self, addr, language):
return self.db.find(addr, language)

def find_map(self, addr, language):
return self.db.find_map(addr, language)

def find_info(self, addr, language):
m = self.db.find_map(addr, language)
if m is None:
return None
return IDCInfo(**m)

def is_ipv4(self):
return self.db.is_support_ipv4()

def is_ipv6(self):
return self.db.is_support_ipv6()

def languages(self):
return self.db.support_languages()

def fields(self):
return self.db.support_fields()

def build_time(self):
return self.db.build_utc_time()
info = IDCInfo
22 changes: 21 additions & 1 deletion ipdb/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:copyright: ©2018 by IPIP.net
"""


import gzip
import sys


Expand All @@ -18,3 +18,23 @@ def convert(v):
return v
else:
return ord(v)


def read_file(path, compression=None):
if compression is None:
return _read_file_default(path)
elif compression == 'gzip':
return _read_file_gzip(path)
raise Exception('unsupported compression type: {}'.format(compression))


def _read_file_default(path):
with open(path, "rb") as f:
content = f.read()
return content


def _read_file_gzip(path):
with gzip.open(path, "rb") as f:
content = f.read()
return content