From c79241027d4918d6e25ab2a55ef73291313b51f0 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 29 Dec 2018 10:31:06 +0800 Subject: [PATCH 1/4] support gzip --- ipdb/database.py | 6 +++--- ipdb/util.py | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ipdb/database.py b/ipdb/database.py index 610de13..263a723 100644 --- a/ipdb/database.py +++ b/ipdb/database.py @@ -8,6 +8,7 @@ import sys from .util import bytes2long +from .util import read_file from .exceptions import NoSupportIPv4Error, NoSupportIPv6Error, NoSupportLanguageError, DatabaseError, IPNotFound @@ -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]) diff --git a/ipdb/util.py b/ipdb/util.py index 637c8f7..cfceb70 100644 --- a/ipdb/util.py +++ b/ipdb/util.py @@ -3,7 +3,7 @@ :copyright: ©2018 by IPIP.net """ - +import gzip import sys @@ -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 From 4a67874b26fa62b11bff648164c22f28bd953c99 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 29 Dec 2018 10:44:52 +0800 Subject: [PATCH 2/4] factor out Database --- ipdb/base_station.py | 44 +++----------------------------------------- ipdb/city.py | 44 +++----------------------------------------- ipdb/database.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ ipdb/district.py | 44 +++----------------------------------------- ipdb/idc.py | 44 +++----------------------------------------- 5 files changed, 56 insertions(+), 164 deletions(-) diff --git a/ipdb/base_station.py b/ipdb/base_station.py index 3a81092..5319b4a 100644 --- a/ipdb/base_station.py +++ b/ipdb/base_station.py @@ -3,7 +3,7 @@ :copyright: ©2018 by IPIP.net """ -from .database import Reader +from .database import Database class BaseStationInfo: @@ -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() \ No newline at end of file + info = BaseStationInfo diff --git a/ipdb/city.py b/ipdb/city.py index 61cb9ff..0089260 100644 --- a/ipdb/city.py +++ b/ipdb/city.py @@ -3,7 +3,7 @@ :copyright: ©2018 by IPIP.net """ -from .database import Reader +from .database import Database class CityInfo: @@ -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() \ No newline at end of file + info = CityInfo diff --git a/ipdb/database.py b/ipdb/database.py index 263a723..36780b0 100644 --- a/ipdb/database.py +++ b/ipdb/database.py @@ -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): + 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 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() diff --git a/ipdb/district.py b/ipdb/district.py index 02a28d8..3d22785 100644 --- a/ipdb/district.py +++ b/ipdb/district.py @@ -3,7 +3,7 @@ :copyright: ©2018 by IPIP.net """ -from .database import Reader +from .database import Database class DistrictInfo: @@ -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() \ No newline at end of file + info = DistrictInfo diff --git a/ipdb/idc.py b/ipdb/idc.py index 486bc4b..61c132c 100644 --- a/ipdb/idc.py +++ b/ipdb/idc.py @@ -3,7 +3,7 @@ :copyright: ©2018 by IPIP.net """ -from .database import Reader +from .database import Database class IDCInfo: @@ -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() \ No newline at end of file + info = IDCInfo From 7ca79081e21a14885b62622fc160b6fc4a95197a Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 29 Dec 2018 10:45:30 +0800 Subject: [PATCH 3/4] support gzip in database --- ipdb/database.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ipdb/database.py b/ipdb/database.py index 36780b0..f6188cd 100644 --- a/ipdb/database.py +++ b/ipdb/database.py @@ -169,12 +169,12 @@ class Database: db = None info = None - def __init__(self, name): - self.db = Reader(name) + def __init__(self, name, compression=None): + self.db = Reader(name, compression=compression) - def reload(self, name): + def reload(self, name, compression=None): try: - db = Reader(name) + db = Reader(name, compression=compression) self.db = db return True except: From fa30ab1b802539909d3e20f594f1b032b2885be3 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 29 Dec 2018 10:51:00 +0800 Subject: [PATCH 4/4] update readme --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 07da554..3fd8020 100644 --- a/README.md +++ b/README.md @@ -75,4 +75,20 @@ print(db.languages()) print(db.fields()) print(db.build_time()) print(db.find_map("117.136.83.55", "CN")) - \ No newline at end of file + + +### 支持加载压缩文件 + +除了直接加载 `ipdb` 格式文件外,还可以加载压缩文件 + +- 支持 `gzip` + +
+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"))
+