https://github.com/synodriver/pyip2region
python binding for ip2region
https://github.com/synodriver/pyip2region
Last synced: 7 months ago
JSON representation
python binding for ip2region
- Host: GitHub
- URL: https://github.com/synodriver/pyip2region
- Owner: synodriver
- Created: 2022-10-20T01:12:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-09-23T15:43:32.000Z (9 months ago)
- Last Synced: 2025-09-23T17:36:49.458Z (9 months ago)
- Language: Cython
- Size: 280 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
- Changelog: changename.py
Awesome Lists containing this project
README
✨ pyip2region ✨
The python binding for ip2region
[](https://pypi.org/project/ip2region/)





### 使用方式
### 完全基于文件的查询
```python
from ip2region import Searcher, VectorIndex, Header, Version
header = Header.from_file(r".\ip2region_v6.xdb")
version = Version.from_header(header)
# 备注:并发使用,每一个线程需要单独定义并且初始化一个 searcher 查询对象,或者加锁
searcher = Searcher.from_file(version, r"E:\pyproject\pyip2region\tests\ip2region_v6.xdb")
result = searcher.search_by_string("2001:0:2851:b9f0:3866:13a2:846f:c23b")
print(result)
```
### 缓存 `VectorIndex` 索引
```python
from ip2region import Searcher, VectorIndex, Header, Version
header = Header.from_file(r".\ip2region_v4.xdb")
version = Version.from_header(header)
index = VectorIndex.from_file(r".\ip2region_v4.xdb")
searcher = Searcher.from_index(version, r".\ip2region_v4.xdb", index)
result = searcher.search_by_string("1.1.1.1")
print(result)
```
### 缓存整个 `xdb` 数据
```python
from ip2region import Searcher, VectorIndex, Header, Version
header = Header.from_file(r".\ip2region_v4.xdb")
version = Version.from_header(header)
with open(r".\ip2region_v4.xdb", "rb") as f:
data = f.read()
searcher = Searcher.from_buffer(version, data)
result = searcher.search_by_string("1.1.1.1")
print(result)
```
### 注意:
多线程模式下,只有纯内存的才是线程安全的,其他都需要用户自己加锁加锁,程序中没有任何机制保证线程安全