Skip to content

Commit 3386fdb

Browse files
committed
Minor updates
- Added supports for DB25. - Added new BIN file open mode.
1 parent 33652f4 commit 3386fdb

File tree

9 files changed

+66
-30
lines changed

9 files changed

+66
-30
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
1.1.0 2021-08-24
2+
* Added supports for DB25.
3+
* Added new BIN file open mode.
4+
15
1.0.0 2019-04-05
26
* Initial release
37
* Support IP2Location IPv6

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# IP2Location-Python-C
22

3-
This is a IP2Location Python-C library that enables the user to find the country, region or state, city, latitude and longitude, ZIP code, time zone, Internet Service Provider (ISP) or company name, domain name, net speed, area code, weather station code, weather station name, mobile country code (MCC), mobile network code (MNC) and carrier brand, elevation, and usage type by IP address or hostname originates from. The library used the **IP2Location C Library** to read the geo location information from **IP2Location BIN data** file.
3+
This is a IP2Location Python-C library that enables the user to find the country, region or state, city, latitude and longitude, ZIP code, time zone, Internet Service Provider (ISP) or company name, domain name, net speed, area code, weather station code, weather station name, mobile country code (MCC), mobile network code (MNC) and carrier brand, elevation, usage type, address type and IAB category by IP address or hostname originates from. The library used the **IP2Location C Library** to read the geo location information from **IP2Location BIN data** file.
44

55
Supported IPv4 and IPv6 address.
66

data/IP-COUNTRY.BIN

-883 KB
Binary file not shown.

data/IP2LOCATION-LITE-DB1.BIN

2.11 MB
Binary file not shown.

data/IP2LOCATION-LITE-DB1.IPV6.BIN

5.31 MB
Binary file not shown.

data/IPV6-COUNTRY.BIN

-2.08 MB
Binary file not shown.

ip2location_python_c.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
* IP2Location C library is distributed under MIT license
3-
* Copyright (c) 2013-2019 IP2Location.com. support@ip2location.com
3+
* Copyright (c) 2013-2021 IP2Location.com. support@ip2location.com
44
*
55
* This library is free software; you can redistribute it and/or
66
* modify it under the terms of the MIT license
@@ -9,12 +9,17 @@
99
from ctypes import *
1010
from ctypes.util import find_library
1111

12+
class IP2Location_lookup_mode :
13+
IP2LOCATION_FILE_IO = 0
14+
IP2LOCATION_CACHE_MEMORY = 1
15+
IP2LOCATION_SHARED_MEMORY = 2
16+
1217

1318
class C_IP2LocationRecord(Structure):
1419
'''
1520
Define the IP2Location Record result structure.
1621
'''
17-
_fields_=[("country_short",c_char_p),("country_long",c_char_p),("region",c_char_p),("city",c_char_p),("isp",c_char_p),("latitude",c_float),("longitude",c_float),("domain",c_char_p),("zipcode",c_char_p),("timezone",c_char_p),("netspeed",c_char_p),("idd_code",c_char_p),("area_code",c_char_p),("weather_code",c_char_p),("weather_name",c_char_p),("mcc",c_char_p),("mnc",c_char_p),("mobile_brand",c_char_p),("elevation",c_float),("usage_type",c_char_p)]
22+
_fields_=[("country_short",c_char_p),("country_long",c_char_p),("region",c_char_p),("city",c_char_p),("isp",c_char_p),("latitude",c_float),("longitude",c_float),("domain",c_char_p),("zipcode",c_char_p),("timezone",c_char_p),("netspeed",c_char_p),("idd_code",c_char_p),("area_code",c_char_p),("weather_code",c_char_p),("weather_name",c_char_p),("mcc",c_char_p),("mnc",c_char_p),("mobile_brand",c_char_p),("elevation",c_float),("usage_type",c_char_p),("address_type",c_char_p),("category",c_char_p)]
1823

1924

2025
class IP2LocationRecord:
@@ -40,6 +45,8 @@ class IP2LocationRecord:
4045
mobile_brand = None
4146
elevation = None
4247
usage_type = None
48+
address_type = None
49+
category = None
4350

4451

4552
class IP2Location(object):
@@ -57,19 +64,23 @@ def __init__(self, filename=None, libraryname = None):
5764
self.ip2location_c = CDLL(find_library('IP2Location'))
5865

5966
def load(self, libraryname):
60-
'''
61-
Function to load the IP2Location C Library if user choose to load their own copy of IP2Location C Library.
62-
'''
67+
'''
68+
Function to load the IP2Location C Library if user choose to load their own copy of IP2Location C Library.
69+
'''
6370
self.ip2location_c = CDLL(libraryname)
6471

65-
def open(self, filename):
72+
def open(self, filename, mode=None):
6673
'''
6774
Function to pass the database name and path to IP2Location_open in IP2Location C library.
6875
Set the argument and response types of the function to avoid data compatibility issue.
6976
'''
77+
if (mode == None):
78+
mode = IP2Location_lookup_mode.IP2LOCATION_FILE_IO
7079
self.ip2location_c.IP2Location_open.argtypes = [c_char_p]
7180
self.ip2location_c.IP2Location_open.restype = c_void_p
81+
self.ip2location_c.IP2Location_open_mem.argtypes = [c_void_p, c_int]
7282
self.ip2location_database_pointer = self.ip2location_c.IP2Location_open(filename)
83+
self.ip2location_c.IP2Location_open_mem(self.ip2location_database_pointer, mode)
7384

7485
def get_country_short(self, ip):
7586
''' Get country_short '''
@@ -151,6 +162,14 @@ def get_usage_type(self, ip):
151162
''' Get usage_type '''
152163
rec = self.get_all(ip)
153164
return rec and rec.usage_type
165+
def get_address_type(self, ip):
166+
''' Get address_type '''
167+
rec = self.get_all(ip)
168+
return rec and rec.address_type
169+
def get_category(self, ip):
170+
''' Get category '''
171+
rec = self.get_all(ip)
172+
return rec and rec.category
154173

155174
def get_all(self, ip):
156175
''' set the argument and response types of the function for data compatibility issue. '''
@@ -179,6 +198,8 @@ def get_all(self, ip):
179198
self.rec.mobile_brand = self.result.contents.mobile_brand
180199
self.rec.elevation = self.result.contents.elevation
181200
self.rec.usage_type = self.result.contents.usage_type
201+
self.rec.address_type = self.result.contents.address_type
202+
self.rec.category = self.result.contents.category
182203
return self.rec
183204

184205
def close(self):

sample.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,41 @@
1111

1212
database = ip2location_python_c.IP2Location()
1313

14-
database.open(os.path.join("data", "IP-COUNTRY.BIN"))
14+
"""
15+
Cache the database into memory to accelerate lookup speed.
16+
WARNING: Please make sure your system have sufficient RAM to use this feature.
17+
Choose either one from below to load the database.
18+
"""
19+
# database.open(os.path.join("data", "IP2LOCATION-LITE-DB1.BIN"), ip2location_python_c.IP2Location_lookup_mode.IP2LOCATION_SHARED_MEMORY)
20+
# database.open(os.path.join("data", "IP2LOCATION-LITE-DB1.BIN"), ip2location_python_c.IP2Location_lookup_mode.IP2LOCATION_CACHE_MEMORY)
21+
22+
# Load the database using FILE I/O mode.
23+
database.open(os.path.join("data", "IP2LOCATION-LITE-DB1.BIN"))
1524

1625
rec = database.get_all(ip)
1726

18-
print rec.country_short
19-
print rec.country_long
20-
print rec.region
21-
print rec.city
22-
print rec.isp
23-
print rec.latitude
24-
print rec.longitude
25-
print rec.domain
26-
print rec.zipcode
27-
print rec.timezone
28-
print rec.netspeed
29-
print rec.idd_code
30-
print rec.area_code
31-
print rec.weather_code
32-
print rec.weather_name
33-
print rec.mcc
34-
print rec.mnc
35-
print rec.mobile_brand
36-
print rec.elevation
37-
print rec.usage_type
38-
print("\nYou may download the DB24 sample BIN at https://www.ip2location.com/downloads/sample6.bin.db24.zip for full data display.")
27+
print("Country Code : " + rec.country_short)
28+
print("Country Name : " + rec.country_long)
29+
print("Region Name : " + rec.region)
30+
print("City Name : " + rec.city)
31+
print("ISP Name : " + rec.isp)
32+
print("Latitude : " + str(rec.latitude))
33+
print("Longitude : " + str(rec.longitude))
34+
print("Domain Name : " + rec.domain)
35+
print("ZIP Code : " + rec.zipcode)
36+
print("Time Zone : " + rec.timezone)
37+
print("Net Speed : " + rec.netspeed)
38+
print("Area Code : " + rec.idd_code)
39+
print("IDD Code : " + rec.area_code)
40+
print("Weather Station Code : " + rec.weather_code)
41+
print("Weather Station Name : " + rec.weather_name)
42+
print("MCC : " + rec.mcc)
43+
print("MNC : " + rec.mnc)
44+
print("Mobile Carrier : " + rec.mobile_brand)
45+
print("Elevation : " + str(rec.elevation))
46+
print("Usage Type : " + rec.usage_type)
47+
print("Address Type : " + rec.address_type)
48+
print("Category : " + rec.category)
49+
print("\nYou may download the DB25 sample BIN at https://www.ip2location.com/downloads/sample6.bin.db25.zip for full data display.")
3950

4051
database.close()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name='ip2location_python_c',
7-
version='1.0.0',
7+
version='1.1.0',
88
description='IP2Location Python library with C module to read geolocation information from IP2Location database',
99
author='IP2Location',
1010
author_email='support@ip2location.com',

0 commit comments

Comments
 (0)