-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathipfind.py
More file actions
120 lines (109 loc) · 3.71 KB
/
ipfind.py
File metadata and controls
120 lines (109 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/env python
# coding:utf-8
from struct import *
import string
def ip2string( ip ):
a = (ip & 0xff000000) >> 24
b = (ip & 0x00ff0000) >> 16
c = (ip & 0x0000ff00) >> 8
d = ip & 0x000000ff
return "%d.%d.%d.%d" % (a,b,c,d)
def string2ip( str ):
ss = string.split(str, '.');
ip = 0L
for s in ss: ip = (ip << 8) + string.atoi(s)
return ip;
class IpLocater :
def __init__( self ):
self.ipdb = open("qqwry.dat", "rb" )
# get index address
str = self.ipdb.read( 8 )
(self.first_index,self.last_index) = unpack('II',str)
self.index_count = (self.last_index - self.first_index) / 7 + 1
def getString(self,offset = 0):
if offset :
self.ipdb.seek( offset )
str = ""
ch = self.ipdb.read( 1 )
(byte,) = unpack('B',ch)
while byte != 0:
str = str + ch
ch = self.ipdb.read( 1 )
(byte,) = unpack('B',ch)
return str
def getLong3(self,offset = 0):
if offset :
self.ipdb.seek( offset )
str = self.ipdb.read(3)
(a,b) = unpack('HB',str)
return (b << 16) + a
def getAreaAddr(self,offset=0):
if offset :
self.ipdb.seek( offset )
str = self.ipdb.read( 1 )
(byte,) = unpack('B',str)
if byte == 0x01 or byte == 0x02:
p = self.getLong3()
if p:
return self.getString( p )
else:
return ""
else:
return self.getString( offset )
def getAddr(self,offset ,ip = 0):
self.ipdb.seek( offset + 4)
countryAddr = ""
areaAddr = ""
str = self.ipdb.read( 1 )
(byte,) = unpack('B',str)
if byte == 0x01:
countryOffset = self.getLong3()
self.ipdb.seek(countryOffset )
str = self.ipdb.read( 1 )
(b,) = unpack('B',str)
if b == 0x02:
countryAddr = self.getString( self.getLong3() )
self.ipdb.seek( countryOffset + 4 )
else:
countryAddr = self.getString( countryOffset )
areaAddr = self.getAreaAddr()
elif byte == 0x02:
countryAddr = self.getString( self.getLong3() )
areaAddr = self.getAreaAddr( offset + 8 )
else:
countryAddr = self.getString( offset + 4 )
areaAddr = self.getAreaAddr( )
return countryAddr + "/" + areaAddr
def output(self, first ,last ):
if last > self.index_count :
last = self.index_count
for index in range(first,last):
offset = self.first_index + index * 7
self.ipdb.seek( offset )
buf = self.ipdb.read( 7 )
(ip,of1,of2) = unpack("IHB",buf)
print "%s - %s" % (ip, self.getAddr( of1 + (of2 << 16) ) )
def find(self,ip,left,right):
if right-left == 1:
return left
else:
middle = ( left + right ) / 2
offset = self.first_index + middle * 7
self.ipdb.seek( offset )
buf = self.ipdb.read( 4 )
(new_ip,) = unpack("I",buf)
if ip <= new_ip :
return self.find( ip, left, middle )
else:
return self.find( ip, middle, right )
#----------------------------------------------------------------------
def cha(self,ip):
""""""
index = self.find( string2ip(ip),0,self.index_count - 1 )
ioffset = self.first_index + index * 7
aoffset = self.getLong3( ioffset + 4)
address = self.getAddr( aoffset )
result=ip+":"+address.decode('gbk')
print address
a=IpLocater()
a.cha("111.12.1.157")