testIPAddress.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import socket
  5. import struct
  6. import logging
  7. logger = logging.getLogger('util')
  8. class IPAddresss:
  9. def __init__(self, ipdbFile):
  10. self.ipdb = open(ipdbFile, "rb")
  11. str = self.ipdb.read(8)
  12. (self.firstIndex, self.lastIndex) = struct.unpack('II', str)
  13. self.indexCount = int((self.lastIndex - self.firstIndex) / 7 + 1)
  14. # print self.getVersion(), u" 纪录总数: %d 条 "%(self.indexCount)
  15. def getVersion(self):
  16. s = self.getIpAddr(0xffffff00)
  17. return s
  18. def getAreaAddr(self, offset=0):
  19. if offset:
  20. self.ipdb.seek(offset)
  21. str = self.ipdb.read(1)
  22. (byte,) = struct.unpack('B', str)
  23. if byte == 0x01 or byte == 0x02:
  24. p = self.getLong3()
  25. if p:
  26. return self.getString(p)
  27. else:
  28. return ""
  29. else:
  30. self.ipdb.seek(-1, 1)
  31. return self.getString(offset)
  32. def getAddr(self, offset, ip=0):
  33. self.ipdb.seek(offset + 4)
  34. countryAddr = ""
  35. areaAddr = ""
  36. str = self.ipdb.read(1)
  37. (byte,) = struct.unpack('B', str)
  38. if byte == 0x01:
  39. countryOffset = self.getLong3()
  40. self.ipdb.seek(countryOffset)
  41. str = self.ipdb.read(1)
  42. (b,) = struct.unpack('B', str)
  43. if b == 0x02:
  44. countryAddr = self.getString(self.getLong3())
  45. self.ipdb.seek(countryOffset + 4)
  46. else:
  47. countryAddr = self.getString(countryOffset)
  48. areaAddr = self.getAreaAddr()
  49. elif byte == 0x02:
  50. countryAddr = self.getString(self.getLong3())
  51. areaAddr = self.getAreaAddr(offset + 8)
  52. else:
  53. countryAddr = self.getString(offset + 4)
  54. areaAddr = self.getAreaAddr()
  55. return countryAddr + " " + areaAddr
  56. def dump(self, first, last):
  57. if last > self.indexCount:
  58. last = self.indexCount
  59. for index in range(first, last):
  60. offset = self.firstIndex + index * 7
  61. self.ipdb.seek(offset)
  62. buf = self.ipdb.read(7)
  63. (ip, of1, of2) = struct.unpack("IHB", buf)
  64. address = self.getAddr(of1 + (of2 << 16))
  65. # 把GBK转为utf-8
  66. address = str(address, 'gbk').encode("utf-8")
  67. logger.info("%d %s %s" % (index, self.ip2str(ip), address))
  68. def setIpRange(self, index):
  69. offset = self.firstIndex + index * 7
  70. self.ipdb.seek(offset)
  71. buf = self.ipdb.read(7)
  72. (self.curStartIp, of1, of2) = struct.unpack("IHB", buf)
  73. self.curEndIpOffset = of1 + (of2 << 16)
  74. self.ipdb.seek(self.curEndIpOffset)
  75. buf = self.ipdb.read(4)
  76. (self.curEndIp,) = struct.unpack("I", buf)
  77. def getIpAddr(self, ip):
  78. L = 0
  79. R = self.indexCount - 1
  80. while L < R - 1:
  81. M = int((L + R) / 2)
  82. self.setIpRange(M)
  83. if ip == self.curStartIp:
  84. L = M
  85. break
  86. if ip > self.curStartIp:
  87. L = M
  88. else:
  89. R = M
  90. self.setIpRange(L)
  91. # version information, 255.255.255.X, urgy but useful
  92. if ip & 0xffffff00 == 0xffffff00:
  93. self.setIpRange(R)
  94. if self.curStartIp <= ip <= self.curEndIp:
  95. address = self.getAddr(self.curEndIpOffset)
  96. # 把GBK转为utf-8
  97. address = str(address)
  98. else:
  99. address = "未找到该IP的地址"
  100. return address
  101. def getIpRange(self, ip):
  102. self.getIpAddr(ip)
  103. range = self.ip2str(self.curStartIp) + ' - ' \
  104. + self.ip2str(self.curEndIp)
  105. return range
  106. def getString(self, offset=0):
  107. if offset:
  108. self.ipdb.seek(offset)
  109. str = b''
  110. ch = self.ipdb.read(1)
  111. (byte,) = struct.unpack('B', ch)
  112. while byte != 0:
  113. str += ch
  114. ch = self.ipdb.read(1)
  115. (byte,) = struct.unpack('B', ch)
  116. return str.decode('gbk')
  117. def ip2str(self, ip):
  118. return str(ip >> 24) + '.' + str((ip >> 16) & 0xff) + '.' + str((ip >> 8) & 0xff) + '.' + str(ip & 0xff)
  119. def str2ip(self, s):
  120. (ip,) = struct.unpack('I', socket.inet_aton(s))
  121. return ((ip >> 24) & 0xff) | ((ip & 0xff) << 24) | ((ip >> 8) & 0xff00) | ((ip & 0xff00) << 8)
  122. def getLong3(self, offset=0):
  123. if offset:
  124. self.ipdb.seek(offset)
  125. str = self.ipdb.read(3)
  126. (a, b) = struct.unpack('HB', str)
  127. return (b << 16) + a
  128. QQWRY_PATH = os.path.dirname(__file__) + "/../data/qqwry.dat"
  129. ips = IPAddresss(QQWRY_PATH)
  130. addr = ips.getIpAddr(ips.str2ip('183.61.236.53'))
  131. print(addr)