This issue features an open-source offline IP address location library called Ip2region.
Ip2region is an offline IP address location library with an accuracy rate of up to 99.9%, and a search performance of 0.0x milliseconds. The DB file is only a few megabytes in size, which stores all IP addresses. It supports queries for binding in Java, PHP, C, Python, Node.js, Go, C#, and Lua. The query algorithm uses binary trees, B-trees, and in-memory search algorithms.
functional characteristics
- Query accuracy up to 99.9%
The data comes from some well-known IP query providers and has been tested to be more accurate than pure IP positioning.
- >80%, Taobao IP address library: http://ip.taobao.com
- ≈10%,GeoIP: https://geoip.com
- ≈2%,Pure IP Library: http://www.cz88.net
- Small file size
The database file ip2region.db is only a few MB in size, with the smallest version not exceeding 1.5MB and the largest not exceeding 8MB.
- standard data format
Each IP data segment has a fixed format, and currently only domestic data can be accurately located at the city level. For other countries, only a portion can be located at the country level, and the default value for data that cannot be confirmed is 0.
_City ID | Country | Region | Province | City | ISP_
- Fast query speed
All client queries are at the 0. x millisecond level and come with three built-in query algorithms:
- Memory algorithm: The entire database is loaded into memory, and a single query occurs within 0.1x milliseconds. For C language clients, a single query occurs at the 0.00x millisecond level.
- Binary algorithm: based on binary search, based on the ip2region.db file, does not require loading into memory, and a single query takes 0. x milliseconds.
- B-tree algorithm: Based on the BTree algorithm and the ip2region.db file, it does not require loading into memory and word queries are faster than binary algorithms at the 0. x millisecond level.
- Support for multiple query clients
The client has integrated Java, C #, PHP, C, Python, NodeJS, PHP extensions (PHP5 and PHP7), Golang, Rust, Lua, Lua_c, and nginx.
Quick installation
- Maven warehouse address
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>
- nodejs
npm install node-ip2region --save
- Nuget installation
Install-Package IP2Region
- php composer
composer require zoujingli/ip2region
Quick testing
Please refer to the README instructions under each binding to run the CLI test program. For example, the demo in C language runs as follows:
cd binding/c/
gcc -g -O2 testSearcher.c ip2region.c
./a.out ../../data/ip2region.db
You will see the following CLI interface:
initializing B-tree ...
+----------------------------------+
| ip2region test scrip
t |
| Author: chenxin619315@gmail.com |
| Type 'quit' to exit program |
+----------------------------------+
p2region>> 101.105.35.57
2163 | China | South China | Guangdong Province | Shenzhen City | Dr. Peng in 0.02295 milliseconds
Enter the IP address to start testing. The first time may be a bit slow. After running the command, connect binary and memory to try other algorithms. It is recommended to use the b-tree algorithm. For speed and concurrency requirements, the memory algorithm can be used. Please refer to the testing source code under different bindings for specific integration.