|
| 1 | +#ipdb-python |
| 2 | +IPIP.net officially supported IP database ipdb format parsing library |
| 3 | + |
| 4 | +#Python Parse ipdb file |
| 5 | + |
| 6 | +##Installing |
| 7 | +<pre> |
| 8 | +<code>pip install ipip-ipdb</code> |
| 9 | +</pre> |
| 10 | + |
| 11 | +##Dependents ( python 2.x or before python 3.3 ) |
| 12 | +<pre><code>pip install ipaddress</code></pre> |
| 13 | + |
| 14 | +##Code Example |
| 15 | + <pre><code> |
| 16 | +import ipdb |
| 17 | + |
| 18 | +db = ipdb.City("/path/to/city.ipv4.ipdb") |
| 19 | +# db.reload("/path/to/city.ipv4.ipdb") # update ipdb database file reload data |
| 20 | +print(db.is_ipv4(), db.is_ipv6()) |
| 21 | +print(db.languages()) # support language |
| 22 | +print(db.fields()) # support fields |
| 23 | +print(db.build_time()) # build database time |
| 24 | +print(db.find("1.1.1.1", "CN")) # query ip return array |
| 25 | +# print(db.find(u"1.1.1.1", "CN")) # Python 2.7 |
| 26 | +print(db.find_map("8.8.8.8", "CN")) # query ip return dict |
| 27 | +print(db.find_info("118.28.1.1", "CN").country_name) |
| 28 | + </pre></code> |
| 29 | + |
| 30 | +###IPDB Database Fields: |
| 31 | +<pre> |
| 32 | +country_name : Country Name |
| 33 | +region_name : Province Name |
| 34 | +city_name : City Name |
| 35 | +owner_domain : Domain of IP owner |
| 36 | +isp_domain : ISP of IP being broadcasted |
| 37 | +latitude : Latitude |
| 38 | +longitude : Longitude |
| 39 | +timezone : Timezone |
| 40 | +utc_offset : UTC time standard offset |
| 41 | +china_admin_code : China administrative divisions code |
| 42 | +idd_code : International Dialling Codes |
| 43 | +country_code : Two-letter country codes based on ISO 3166. |
| 44 | +continent_code : Regional Internet registry code. |
| 45 | +idc : IDC label |
| 46 | +base_station : Basestation label |
| 47 | +country_code3 : Three-letter country codes based on ISO 3166. |
| 48 | +european_union : European union label |
| 49 | +currency_code : Currency Code |
| 50 | +currency_name : Currency Name |
| 51 | +anycast : ANYCAST label |
| 52 | + |
| 53 | +</pre> |
| 54 | + |
| 55 | +###IPDB CN District sample code : |
| 56 | + <pre> |
| 57 | +import ipdb |
| 58 | + |
| 59 | +db = ipdb.District("/path/to/china_district.ipdb") |
| 60 | +print(db.is_ipv4(), db.is_ipv6()) |
| 61 | +print(db.languages()) |
| 62 | +print(db.fields()) |
| 63 | +print(db.build_time()) |
| 64 | +print(db.find("1.12.13.255", "CN")) |
| 65 | +print(db.find_map("1.12.13.255", "CN")) |
| 66 | +print(db.find_info("1.12.13.255", "CN").country_name) |
| 67 | + </pre> |
| 68 | + |
| 69 | +###IPDB Basestation sample code : |
| 70 | +<pre> |
| 71 | +import ipdb |
| 72 | +db = ipdb.BaseStation("/path/to/base_station.ipdb") |
| 73 | +print(db.is_ipv4(), db.is_ipv6()) |
| 74 | +print(db.languages()) |
| 75 | +print(db.fields()) |
| 76 | +print(db.build_time()) |
| 77 | +print(db.find_map("117.136.83.55", "CN")) |
| 78 | +</pre> |