IP地址批量查詢
# E:/devspace/python/test/spider/ip.py
# -*- coding: GBK -*-
from binascii import b2a_hex
# Hex to int
def Hex2Int(data):
i0 = int(b2a_hex(data[0]), 16)
i1 = int(b2a_hex(data[1]), 16)
i2 = int(b2a_hex(data[2]), 16)
i3 = int(b2a_hex(data[3]), 16)
l = i0 & 0xFF
l |= (i1 << 8) & 0xFF00
l |= (i2 << 16) & 0xFF0000
l |= (i3 << 24) & 0xFF000000
return l
# yet aother Hex2Int
def Hex2Int3(data):
i0 = int(b2a_hex(data[0]), 16)
i1 = int(b2a_hex(data[1]), 16)
i2 = int(b2a_hex(data[2]), 16)
l = i0 & 0xFF
l |= (i1 << 8) & 0xFF00
l |= (i2 << 16) & 0xFF0000
return l
# change ip to name. such 192.169.1.1
def IP2Name(ip):
ip0 = ip & 0xFF
ip1 = (ip >> 8) & 0xFF
ip2 = (ip >> 16) & 0xFF
ip3 = (ip >> 24) & 0xFF
return '%d.%d.%d.%d' % (ip3, ip2, ip1, ip0)
class IP2Address:
def __init__(self, filename):
try:
self.data_file = open(filename, 'rb')
index = self.data_file.read(4)
self.index_begin = Hex2Int(index) # begin index
index = self.data_file.read(4)
self.index_end = Hex2Int(index) # end index
self.indexCount = (self.index_end - self.index_begin) / 7
print "qqwry.bat紀錄總數: %d 條 "%(self.indexCount + 1)
except Exception, msg:
print msg
def Close(self):
if self.data_file:
self.data_file.close()
def GetMode(self, offset):
""" get the mode """
self.data_file.seek(offset)
return int(b2a_hex(self.data_file.read(1)), 16)
def GetString(self, offset):
""" get string in the fiile """
self.data_file.seek(offset)
onechar = self.data_file.read(1)
charval = int(b2a_hex(onechar), 16)
str = ""
while charval != 0:
str += onechar
onechar = self.data_file.read(1)
charval = int(b2a_hex(onechar), 16)
return str
def GetArea(self, offset):
mode = self.GetMode(offset)
area = "unknown"
if mode == 0x01 or mode == 0x02:
pos = Hex2Int3(self.data_file.read(3))
if pos != 0:
area = self.GetString(pos)
else:
area = self.GetString(offset)
return area
def GetAddress(self, offset):
mode = self.GetMode(offset + 4)
nation = ""
address = ""
if mode == 0x01:
pos = Hex2Int3(self.data_file.read(3))
mode = self.GetMode(pos)
if mode == 0x02:
nation_pos = Hex2Int3(self.data_file.read(3))
nation = self.GetString(nation_pos)
self.data_file.seek(pos + 4)
else:
nation = self.GetString(pos)
address = self.GetArea(self.data_file.tell())
elif mode == 0x02:
nation_pos = Hex2Int3(self.data_file.read(3))
nation = self.GetString(nation_pos)
address = self.GetArea(offset + 4)
else:
nation = self.GetString(offset + 4)
address = self.GetArea(self.data_file.tell())
return (nation, address)
def GetIPAddress(self, ip):
#such as ip: 24.86.118.0
ips = ip.split('.')
theIP = (int(ips[0]) << 24) | (int(ips[1]) << 16) /
| (int(ips[2]) << 8) | (int(ips[3]))
index = self.SearchIP(theIP, 0, self.indexCount)
offset = self.index_begin + index * 7 + 4
self.data_file.seek(offset)
pos = Hex2Int3(self.data_file.read(4))
return self.GetAddress(pos)
def SearchIP(self, ip, begin, end):
# begin, end is the IP index. Return the IP index
mid = (begin + end) / 2
if mid - begin <=1: # only 0 or 1 is ok
return mid
self.data_file.seek(self.index_begin + mid * 7)
mid_ip = Hex2Int(self.data_file.read(4))
if ip < mid_ip:
return self.SearchIP(ip, begin, mid)
else:
return self.SearchIP(ip, mid, end)
if __name__ == "__main__":
import time
begin = time.time()
data = IP2Address("F://Program Files//cz88.net//ip//qqwry.dat")
fi = open('ip.txt', 'r')
fo = open('ipAddress.txt', 'w')
lines = fi.readlines()
for ip in lines:
try:
ip = ip.split()[0]
nation, address = data.GetIPAddress(ip)
fo.write('%s/t%s/t%s/n'%(ip, nation, address))
except Exception, e:
print ip
print e
fi.close()
fo.close()
data.Close()
print 'Time:', time.time() - begin