Coder Social home page Coder Social logo

ipdb-python's Introduction

ipdb-python

IPIP.net officially supported IP database ipdb format parsing library

Python Parse ipdb file

Installing

pip install ipip-ipdb

Dependents ( python 2.x or before python 3.3 )

pip install ipaddress

Code Example

适用于IPDB格式的每周高级版,每日标准版,每日高级版,每日专业版,每日旗舰版


import ipdb

db = ipdb.City("/path/to/city.ipv4.ipdb")
# db.reload("/path/to/city.ipv4.ipdb") # update ipdb database file reload data
print(db.is_ipv4(), db.is_ipv6())
print(db.languages()) # support language
print(db.fields()) #  support fields
print(db.build_time()) #  build database time
print(db.find("1.1.1.1", "CN")) #  query ip return array
# print(db.find(u"1.1.1.1", "CN")) #  Python 2.7
print(db.find_map("8.8.8.8", "CN")) #  query ip return dict
print(db.find_info("118.28.1.1", "CN").country_name) 
db.find_info("118.28.1.1", "CN").get_asninfo()
  

地级市精度库数据字段说明

country_name : 国家名字 (每周高级版及其以上版本包含)
region_name  : 省名字   (每周高级版及其以上版本包含)
city_name    : 城市名字 (每周高级版及其以上版本包含)
owner_domain : 所有者   (每周高级版及其以上版本包含)
isp_domain  : 运营商 (每周高级版与每日高级版及其以上版本包含)
latitude  :  纬度   (每日标准版及其以上版本包含)
longitude : 经度    (每日标准版及其以上版本包含)
timezone : 时区     (每日标准版及其以上版本包含)
utc_offset : UTC时区    (每日标准版及其以上版本包含)
china_admin_code : **行政区划代码 (每日标准版及其以上版本包含)
idd_code : 国家电话号码前缀 (每日标准版及其以上版本包含)
country_code : 国家2位代码  (每日标准版及其以上版本包含)
continent_code : 大洲代码   (每日标准版及其以上版本包含)
idc : IDC |  VPN   (每日专业版及其以上版本包含)
base_station : 基站 | WIFI (每日专业版及其以上版本包含)
country_code3 : 国家3位代码 (每日专业版及其以上版本包含)
european_union : 是否为欧盟成员国: 1 | 0 (每日专业版及其以上版本包含)
currency_code : 当前国家货币代码    (每日旗舰版及其以上版本包含)
currency_name : 当前国家货币名称    (每日旗舰版及其以上版本包含)
anycast : ANYCAST       (每日旗舰版及其以上版本包含)

适用于IPDB格式的**地区 IPv4 区县库

import ipdb

db = ipdb.District("/path/to/china_district.ipdb")
print(db.is_ipv4(), db.is_ipv6())
print(db.languages())
print(db.fields())
print(db.build_time())
print(db.find("1.12.13.255", "CN"))
print(db.find_map("1.12.13.255", "CN"))
print(db.find_info("1.12.13.255", "CN").country_name)
  

适用于IPDB格式的 IDC 库

import ipdb
>>> db = ipdb.IDC("/path/to/idc_list.ipdb") 
>>> print db.find_info(u"8.142.10.33", "CN").isp_domain
aliyun.com
>>> print db.find_info(u"8.142.10.33", "CN").idc
IDC

适用于IPDB格式的基站 IPv4 库

import ipdb
db = ipdb.BaseStation("/path/to/base_station.ipdb")
print(db.is_ipv4(), db.is_ipv6())
print(db.languages())
print(db.fields())
print(db.build_time())
print(db.find_map("117.136.83.55", "CN"))

ipdb-python's People

Contributors

ilsanbao avatar ipipdotnet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ipdb-python's Issues

ipaddress.AddressValueError on RHEL 7.6

python-ipaddress.noarch             1.0.16-2.el7     @anaconda/7.5
$ python
Python 2.7.5 (default, Sep 12 2018, 05:31:16) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipdb
>>> db = ipdb.City("17monipdb.ipdb")
>>> print(db.is_ipv4(), db.is_ipv6())
(True, False)
>>> print(db.languages())
[u'CN']
>>> print(db.fields())
[u'country_name', u'region_name', u'city_name', u'owner_domain', u'isp_domain', u'latitude', u'longitude', u'timezone', u'utc_offset', u'china_admin_code', u'idd_code', u'country_code', u'continent_code']
>>> print(db.build_time())
1542805875
>>> print(db.find("1.1.1.1", "CN"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/ipdb/city.py", line 56, in find
    return self.db.find(addr, language)
  File "/usr/lib/python2.7/site-packages/ipdb/database.py", line 112, in find
    ipv = ipaddress.ip_address(addr)
  File "/usr/lib/python2.7/site-packages/ipaddress.py", line 163, in ip_address
    ' a unicode object?' % address)
ipaddress.AddressValueError: '1.1.1.1' does not appear to be an IPv4 or IPv6 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?
>>> print(db.find_map("8.8.8.8", "CN"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/ipdb/city.py", line 59, in find_map
    return self.db.find_map(addr, language)
  File "/usr/lib/python2.7/site-packages/ipdb/database.py", line 136, in find_map
    loc = self.find(addr, language)
  File "/usr/lib/python2.7/site-packages/ipdb/database.py", line 112, in find
    ipv = ipaddress.ip_address(addr)
  File "/usr/lib/python2.7/site-packages/ipaddress.py", line 163, in ip_address
    ' a unicode object?' % address)
ipaddress.AddressValueError: '8.8.8.8' does not appear to be an IPv4 or IPv6 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?
>>> print(db.find_info("118.28.1.1", "CN").country_name) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/ipdb/city.py", line 62, in find_info
    m = self.db.find_map(addr, language)
  File "/usr/lib/python2.7/site-packages/ipdb/database.py", line 136, in find_map
    loc = self.find(addr, language)
  File "/usr/lib/python2.7/site-packages/ipdb/database.py", line 112, in find
    ipv = ipaddress.ip_address(addr)
  File "/usr/lib/python2.7/site-packages/ipaddress.py", line 163, in ip_address
    ' a unicode object?' % address)
ipaddress.AddressValueError: '118.28.1.1' does not appear to be an IPv4 or IPv6 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?
>>> exit()

PyPI 上的 1.6.1 版本不在此仓库中

PyPI 上的 1.6.1 版本包含对 Python 3.7、3.8 声明支持,修改了 ipdb/database.py 中的错误信息等变动,这些变动均未在此仓库中同步,也没有对应的 1.6.1 tag。

查找的时候报错cannot access class sun.net.util.IPAddressUtil

        City db = new City("ip.ipdb");
        System.out.println(Arrays.toString(db.find("1.1.1.1", "CN")));

会报错
Exception in thread "main" java.lang.IllegalAccessError: class net.ipip.ipdb.Reader (in unnamed module @0x5479e3f) cannot access class sun.net.util.IPAddressUtil (in module java.base) because module java.base does not export sun.net.util to unnamed module @0x5479e3f
at net.ipip.ipdb.Reader.find(Reader.java:100)
at net.ipip.ipdb.City.find(City.java:36)
at org.example.Main.main(Main.java:23)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.