1+
12import os
23from binascii import hexlify
34from socket import error , inet_aton , inet_pton , socket
@@ -21,13 +22,7 @@ class DBReader:
2122 COLUNN_HEADER = "<P23xB" #"x%s/a23name/Cvalue" null 23null-padded_string UnsigedChar <x23xB
2223 HEADERS = "<BB3BHL"
2324
24- handler = None
25- tree_start = None
26- tree_end = None
27- record_bytes = None
28- columns = []
29- ipv6 = False
30- blacklistfile = False
25+
3126
3227 def __init__ (self , filename :str ):
3328 if not os .path .isfile (filename ):
@@ -38,6 +33,13 @@ def __init__(self, filename:str):
3833 except IOError :
3934 raise FileReaderException ('Invalid or non existant file name specified. Please check the file and try again' )
4035
36+ self .tree_start = None
37+ self .tree_end = None
38+ self .record_bytes = None
39+ self .columns = []
40+ self .ipv6 = False
41+ self .blacklistfile = False
42+
4143 self .SetupHeaders ()
4244 self .SetupColumns ()
4345 self .SetupTreeHeaders ()
@@ -63,7 +65,7 @@ def Fetch(self, ip:str):
6365 if len (v_literal ) <= position :
6466 raise IPNotFoundException ("Invalid or nonexistant IP address specified for lookup. (EID: 8)" )
6567
66- if v_literal [position ] == "0" :
68+ if v_literal [position ] == 0 :
6769 pos = self .ReadAt (file_position , self .TREE_BYTE_WIDTH )
6870 if len (pos ) == 4 :
6971 file_position = unpack ("<L" , pos )[0 ]
@@ -75,15 +77,11 @@ def Fetch(self, ip:str):
7577 if (self .blacklistfile == False ):
7678 if (file_position == 0 ):
7779 for i in range (position ):
78- if (v_literal [position - i ] == "1" ):
79- l = list (v_literal )
80- l [position - i ] = "0"
81- v_literal = '' .join (l )
80+ if (v_literal [position - i ] == 1 ):
81+ l [position - i ] = 0
8282
8383 for n in range (position - i + 1 , len (v_literal )):
84- l = list (v_literal )
85- l [n ] = "1"
86- v_literal = '' .join (l )
84+ l [n ] = 1
8785
8886 position = position - i
8987 file_position = previous [position ]
@@ -191,14 +189,13 @@ def SetupTreeHeaders(self):
191189 self .tree_end = tree ['tree_bytes' ] + self .tree_start
192190
193191 def IP2Literal (self ,ip ):
194- result = ""
195- if (self .ipv6 ):
192+ result = []
193+ if (self .ipv6 ):
196194 for block in self .Expand (ip ).split (":" ):
197- result += bin (int (block ,16 ))[2 :]
195+ for x in bin (int (block , 16 ))[2 :] : result . append (( x ))
198196 else :
199197 for block in ip .split ("." ):
200- result += bin (int (block ,10 ))[2 :].zfill (8 )
201-
198+ for x in bin (int (block , 10 ))[2 :].zfill (8 ) : result .append (int (x ))
202199 return result
203200
204201 def CreateRecord (self ,raw ):
@@ -233,6 +230,7 @@ def CreateRecord(self,raw):
233230 current_byte += 4
234231
235232 elif column .Name () == "Longitude" :
233+
236234 value = unpack ("<f" , raw [current_byte :current_byte + 4 ])[0 ]
237235 record .Longitude (float (value ))
238236 current_byte += 4
@@ -287,4 +285,5 @@ def IsIPv6(self, ip:str):
287285 return False
288286
289287 def __del__ (self ):
290- self .handler .close ()
288+ if self .handler :
289+ self .handler .close ()
0 commit comments