Python's code to access the pure IP database

Source: Internet
Author: User
Core code:

#!/usr/bin/env python #-*-coding:utf-8-*-from bisect import bisect _list1, _list2 = [], [] _init = False Ip2int = Lam BDA Ip_str:reduce (Lambda A, B: (a << 8) + b, [int (i) for I in Ip_str.split ('. ')]) def _init (): Global _list, _init If not _init:for l in open (' ipdata.txt ', ' RB '): ip1, ip2 = L.split () [: 2] addr = '. Join (L.split () [2:]) ip1, ip2 = Ip2int  (IP1), Ip2int (IP2) _list1.append (IP1) _list2.append ((ip1, ip2, addr)) _init = True def ip_from (IP): _init () i = Ip2int (IP) IDX = bisect (_list1, i) assert (idx > 0) If Len (_list1) <= idx:return u ' unknown IP address%s '% IP else:frm, to, addr = _list2[idx-1] If frm <= i <= to:return addr else:return u ' unknown IP address%s '% IP if __name__ = = ' __m Ain__ ': Print ip_from (' 115.238.54.106 ') print ip_from (' 220.181.29.160 ') print ip_from (' 115.238.54.107 ') print Ip_from ( ' 8.8.8.8 ')

Code package Download http://xiazai.bitsCN.com/201105/yuanma/ipaddress.7z

Next, we'll share a more perfect code:

#!/usr/bin/env python# coding:utf-8 "Using a Python script to query the format of the Pure IP library QQWry.Dat is as follows: +----------+| File header | (8 bytes) +----------+| Record Area | (indefinite length) +----------+| Index Area | (Size is determined by the file header) +----------+ file header: 4 byte start index offset value + 4 byte end index offset value record area: Each IP record format ==> IP address [country information] [region information] for national records, there can be three representations: string form (IP record 5 bytes is not equal to 0x01 and 0x02), redirect Mode 1 (5th byte is 0x01), then 3 bytes is the offset value redirection mode of the National Information Store (5th byte is 0x02), there can be two representations for the region Record: string form and redirect the last rule: Redirection Mode 1 national records cannot be followed with the region Record index area: Each index record format ==> 4 bytes Start IP address + 3 bytes point to the IP record's offset value the IP of the index area and the IP that it points to the record area in one record to form an IP range.  The query information is in this range IP information ' ' ' Import sysimport socketfrom struct Import pack, unpack Class Ipinfo (object): ' QQWry.Dat database query function collection ' Def __init__ (self, dbname): ' Initializes the class, reads the database contents as a string, determines the index information of the database by starting with 8 bytes ' self.dbname = dbname # f = fil E (dbname, ' R ') # Demon Note: Using ' R ' in Windows will have a problem and will convert \ r \ n to \ n # see Http://demon.tw/programming/python-open-mode.html # and The file function is not advocated in Python documents and is recommended with open F = open (dbname, ' rb ') self.img = F.read () f.close () # QQWry.Dat The beginning of the file 8 bytes is the index Information, the first 4 bytes is the offset value of the start index, # 4The byte is the offset value of the end index. # (Self.firstindex, Self.lastindex) = Unpack (' II ', Self.img[:8]) # Demon Note: Unpack is used by default endian is machine-related # Intel x86 and AMD6 4 (x86-64) is Little-endian # Motorola 68000 and PowerPC G5 is Big-endian # and the Pure database all uses Little-endian byte order # So in some Big-endian machine The original code will go wrong (self.firstindex, self.lastindex) = Unpack ('
 
  

Querying the Pure IP library QQWry.dat with a Python script (demon modified version)

Because to use Python to read a and pure IP database QQWry.dat format similar to the IPV6 database, so in the online search, in Linuxtoy saw a Python script, found that there are some small problems, and then modified a bit.

#!/usr/bin/env python# coding:utf-8# from:http://linuxtoy.org/files/pyip.py# blog:http://linuxtoy.org/archives/ python-ip.html# Modified by demon# blog:http://demon.tw/programming/python-qqwry-dat.html "" Query the format of the pure IP library QQWry.Dat with a Python script as follows: +----------+| File header | (8 bytes) +----------+| Record Area | (indefinite length) +----------+| Index Area | (Size is determined by the file header) +----------+ file header: 4 byte start index offset value + 4 byte end index offset value record area: Each IP record format ==> IP address [country information] [area information] for national records, there can be three representations: string form ( IP record 5th byte is not equal to 0x01 and 0x02 case), redirection mode 1 (5th byte is 0x01), then the next 3 bytes for the national information store of the offset value redirection mode (5th byte is 0x02), for the region record, there can be two ways: string form and redirect last One rule: Redirection mode 1 national records cannot be followed with the region Record index area: Each index record format ==> 4 bytes Starting IP address + 3 bytes point to the IP record of the offset value index area of the IP and it points to the record area of the IP in one record to form an IP range. The query information is in this range IP information ' ' ' Import sysimport socketfrom struct import pack, Unpackclass ipinfo (object): ' QQWry.Dat database query function set ' ' Def __init__ (self, dbname): ' Initializes the class, reads the database contents as a string, determines the index information of the database by starting with 8 bytes ' self.dbname = dbname # f = fil E (dbname, ' R ') # Demon Note: Using ' R ' in Windows will have a problem and will convert \ r \ n to \ n # See Http://demon.tw/programming/python-open-mode.hTML # There are also Python documents that do not advocate opening files with the file function, recommended with open F = open (dbname, ' rb ') self.img = F.read () f.close () # QQWry.Dat file    The beginning of the 8 bytes is the index information, the first 4 bytes is the offset value of the start index, # The last 4 bytes is the offset value of the end index. # (Self.firstindex, Self.lastindex) = Unpack (' II ', Self.img[:8]) # Demon Note: Unpack is used by default endian is machine-related # Intel x86 and AMD64 (x86-64) is Little-endian # Motorola 68000 and PowerPC G5 is Big-endian # and the Pure database all uses Little-endian byte order # so on some Big-endian machines The original code will go wrong (self.firstindex, self.lastindex) = Unpack ('
   
    
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.