Skip to content

Commit 4a0e33e

Browse files
committed
Added support for AS domain, AS usage type and AS CIDR
1 parent ad23b00 commit 4a0e33e

File tree

6 files changed

+80
-32
lines changed

6 files changed

+80
-32
lines changed

IP2Location/database.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
_ADDRESSTYPE_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 21)
3434
_CATEGORY_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 22)
3535
_DISTRICT_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23)
36-
_ASN_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24)
37-
_AS_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25)
36+
_ASN_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24)
37+
_AS_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25)
38+
_ASDOMAIN_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26)
39+
_ASUSAGETYPE_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27)
40+
_ASCIDR_POSITION = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28)
3841

3942
if sys.version < '3':
4043
import urllib, httplib
@@ -156,6 +159,9 @@ class IP2LocationRecord:
156159
district = "This parameter is unavailable in selected .BIN data file. Please upgrade data file."
157160
asn = "This parameter is unavailable in selected .BIN data file. Please upgrade data file."
158161
as_name = "This parameter is unavailable in selected .BIN data file. Please upgrade data file."
162+
as_domain = "This parameter is unavailable in selected .BIN data file. Please upgrade data file."
163+
as_usagetype = "This parameter is unavailable in selected .BIN data file. Please upgrade data file."
164+
as_cidr = "This parameter is unavailable in selected .BIN data file. Please upgrade data file."
159165

160166
def __str__(self):
161167
return str(self.__dict__)
@@ -335,6 +341,18 @@ def get_as(self, ip):
335341
''' Get as_name '''
336342
rec = self.get_all(ip)
337343
return rec and rec.as_name
344+
def get_asdomain(self, ip):
345+
''' Get as_domain '''
346+
rec = self.get_all(ip)
347+
return rec and rec.as_domain
348+
def get_asusagetype(self, ip):
349+
''' Get as_usagetype '''
350+
rec = self.get_all(ip)
351+
return rec and rec.as_usagetype
352+
def get_ascidr(self, ip):
353+
''' Get as_cidr '''
354+
rec = self.get_all(ip)
355+
return rec and rec.as_cidr
338356

339357
def get_all(self, addr):
340358
''' Get the whole record with all fields read from the file
@@ -492,6 +510,15 @@ def calc_off(what, mid):
492510
if _AS_POSITION[self._dbtype] != 0:
493511
rec.as_name = self._reads(struct.unpack('<I', raw_positions_row[((_AS_POSITION[self._dbtype]-1) * 4 - 4) : ((_AS_POSITION[self._dbtype]-1) * 4)])[0] + 1)
494512

513+
if _ASDOMAIN_POSITION[self._dbtype] != 0:
514+
rec.as_domain = self._reads(struct.unpack('<I', raw_positions_row[((_ASDOMAIN_POSITION[self._dbtype]-1) * 4 - 4) : ((_ASDOMAIN_POSITION[self._dbtype]-1) * 4)])[0] + 1)
515+
516+
if _ASUSAGETYPE_POSITION[self._dbtype] != 0:
517+
rec.as_usagetype = self._reads(struct.unpack('<I', raw_positions_row[((_ASUSAGETYPE_POSITION[self._dbtype]-1) * 4 - 4) : ((_ASUSAGETYPE_POSITION[self._dbtype]-1) * 4)])[0] + 1)
518+
519+
if _ASCIDR_POSITION[self._dbtype] != 0:
520+
rec.as_cidr = self._reads(struct.unpack('<I', raw_positions_row[((_ASCIDR_POSITION[self._dbtype]-1) * 4 - 4) : ((_ASCIDR_POSITION[self._dbtype]-1) * 4)])[0] + 1)
521+
495522
return rec
496523

497524
def __iter__(self):

docs/source/code.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ Retrieve geolocation information for an IP address.
4545
| district | District or county name. |
4646
| asn | Autonomous system number (ASN). BIN databases. |
4747
| as_name | Autonomous system (AS) name. |
48+
| as_domain | Domain name of the AS registrant. |
49+
| as_usagetype | Usage type of the AS registrant. |
50+
| as_cidr | CIDR range for the whole AS. |
4851
```
4952

5053
## IP2LocationIPTools Class

docs/source/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
# -- Project information
77

88
project = 'IP2Location'
9-
copyright = '2023, IP2Location'
9+
copyright = '2025, IP2Location'
1010
author = 'IP2Location'
1111

12-
release = '8.10.0'
13-
version = '8.10.0'
12+
release = '8.11.0'
13+
version = '8.11.0'
1414

1515
# -- General configuration
1616

docs/source/quickstart.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pip install IP2Location
3535

3636
For Arch Linux user, you can install the module using the following command:
3737
```Bash
38-
git clone https://aur.archlinux.org/ip2location-python.git && cd ip2location-io-python
38+
git clone https://aur.archlinux.org/ip2location-python.git && cd ip2location-python
3939
makepkg -si
4040
```
4141

@@ -86,12 +86,15 @@ print("Category : " + rec.category)
8686
print("District : " + rec.district)
8787
print("ASN : " + rec.asn)
8888
print("AS : " + rec.as_name)
89+
print("AS Domain : " + rec.as_domain)
90+
print("AS Usage Type : " + rec.as_usagetype)
91+
print("AS CIDR : " + rec.as_cidr)
8992
print("\nYou may download the DB26 sample BIN at http://www.ip2location.com/downloads/sample6.bin.db26.zip for full data display.")
9093
```
9194

9295
### Processing IP address using IP Tools class
9396

94-
You can manupulate IP address, IP number and CIDR as below:
97+
You can manipulate IP address, IP number and CIDR as below:
9598

9699
``` python
97100
import IP2Location

setup.py

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

66
setuptools.setup(
77
name="IP2Location",
8-
version="8.10.5",
8+
version="8.11.0",
99
author="IP2Location",
1010
author_email="[email protected]",
1111
description="This is an IP geolocation library that enables the user to find the country, region, city, latitude and longitude, ZIP code, time zone, ISP, domain name, area code, weather info, mobile info, elevation, usage type, address type and IAB category from an IP address. It supports both IPv4 and IPv6 lookup.",

tests/test_database.py

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def testinvaliddatabase():
1616

1717
def testfunctionexist():
1818
database = IP2Location.IP2Location(ipv4database)
19-
functions_list = ['open', 'close', 'get_all', 'get_country_short', 'get_country_long', 'get_region', 'get_city', 'get_latitude', 'get_longitude', 'get_isp', 'get_domain', 'get_zipcode', 'get_timezone', 'get_netspeed', 'get_idd_code', 'get_area_code', 'get_weather_code', 'get_weather_name', 'get_mcc', 'get_mnc', 'get_mobile_brand', 'get_elevation', 'get_usage_type', 'get_district', 'get_asn', 'get_as']
19+
functions_list = ['open', 'close', 'get_all', 'get_country_short', 'get_country_long', 'get_region', 'get_city', 'get_latitude', 'get_longitude', 'get_isp', 'get_domain', 'get_zipcode', 'get_timezone', 'get_netspeed', 'get_idd_code', 'get_area_code', 'get_weather_code', 'get_weather_name', 'get_mcc', 'get_mnc', 'get_mobile_brand', 'get_elevation', 'get_usage_type', 'get_district', 'get_asn', 'get_as', 'get_asdomain', 'get_asusagetype', 'get_ascidr']
2020
for x in range(len(functions_list)):
21-
assert hasattr(database, functions_list[x]) == True, "Function did not exist."
21+
assert hasattr(database, functions_list[x]) == True, f"Function {functions_list[x]} did not exist."
2222

2323
def testipv4countrycode():
2424
database = IP2Location.IP2Location(ipv4database)
@@ -43,107 +43,122 @@ def testgetcountrylong():
4343
def testgetregion():
4444
database = IP2Location.IP2Location(ipv4database)
4545
rec = database.get_region("8.8.8.8")
46-
assert rec == None
46+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
4747

4848
def testgetcity():
4949
database = IP2Location.IP2Location(ipv4database)
5050
rec = database.get_city("8.8.8.8")
51-
assert rec == None
51+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
5252

5353
def testgetlatitude():
5454
database = IP2Location.IP2Location(ipv4database)
5555
rec = database.get_latitude("8.8.8.8")
56-
assert rec == None
56+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
5757

5858
def testgetlongitude():
5959
database = IP2Location.IP2Location(ipv4database)
6060
rec = database.get_longitude("8.8.8.8")
61-
assert rec == None
61+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
6262

6363
def testgetisp():
6464
database = IP2Location.IP2Location(ipv4database)
6565
rec = database.get_isp("8.8.8.8")
66-
assert rec == None
66+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
6767

6868
def testgetdomain():
6969
database = IP2Location.IP2Location(ipv4database)
7070
rec = database.get_domain("8.8.8.8")
71-
assert rec == None
71+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
7272

7373
def testgetzipcode():
7474
database = IP2Location.IP2Location(ipv4database)
7575
rec = database.get_zipcode("8.8.8.8")
76-
assert rec == None
76+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
7777

7878
def testgettimezone():
7979
database = IP2Location.IP2Location(ipv4database)
8080
rec = database.get_timezone("8.8.8.8")
81-
assert rec == None
81+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
8282

8383
def testgetnetspeed():
8484
database = IP2Location.IP2Location(ipv4database)
8585
rec = database.get_netspeed("8.8.8.8")
86-
assert rec == None
86+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
8787

8888
def testgetiddcode():
8989
database = IP2Location.IP2Location(ipv4database)
9090
rec = database.get_idd_code("8.8.8.8")
91-
assert rec == None
91+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
9292

9393
def testgetareacode():
9494
database = IP2Location.IP2Location(ipv4database)
9595
rec = database.get_area_code("8.8.8.8")
96-
assert rec == None
96+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
9797

9898
def testgetweathercode():
9999
database = IP2Location.IP2Location(ipv4database)
100100
rec = database.get_weather_code("8.8.8.8")
101-
assert rec == None
101+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
102102

103103
def testgetweathername():
104104
database = IP2Location.IP2Location(ipv4database)
105105
rec = database.get_weather_name("8.8.8.8")
106-
assert rec == None
106+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
107107

108108
def testgetmcc():
109109
database = IP2Location.IP2Location(ipv4database)
110110
rec = database.get_mcc("8.8.8.8")
111-
assert rec == None
111+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
112112

113113
def testgetmnc():
114114
database = IP2Location.IP2Location(ipv4database)
115115
rec = database.get_mnc("8.8.8.8")
116-
assert rec == None
116+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
117117

118118
def testgetmobilebrand():
119119
database = IP2Location.IP2Location(ipv4database)
120120
rec = database.get_mobile_brand("8.8.8.8")
121-
assert rec == None
121+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
122122

123123
def testgetelevation():
124124
database = IP2Location.IP2Location(ipv4database)
125125
rec = database.get_elevation("8.8.8.8")
126-
assert rec == None
126+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
127127

128128
def testgetusagetype():
129129
database = IP2Location.IP2Location(ipv4database)
130130
rec = database.get_usage_type("8.8.8.8")
131-
assert rec == None
131+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
132132

133133
def testgetdistrict():
134134
database = IP2Location.IP2Location(ipv4database)
135135
rec = database.get_district("8.8.8.8")
136-
assert rec == None
136+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
137137

138138
def testgetasn():
139139
database = IP2Location.IP2Location(ipv4database)
140140
rec = database.get_asn("8.8.8.8")
141-
assert rec == None
141+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
142142

143143
def testgetas():
144144
database = IP2Location.IP2Location(ipv4database)
145145
rec = database.get_as("8.8.8.8")
146-
assert rec == None
146+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
147+
148+
def testgetasdomain():
149+
database = IP2Location.IP2Location(ipv4database)
150+
rec = database.get_asdomain("8.8.8.8")
151+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
152+
153+
def testgetasusagetype():
154+
database = IP2Location.IP2Location(ipv4database)
155+
rec = database.get_asusagetype("8.8.8.8")
156+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
157+
158+
def testgetascidr():
159+
database = IP2Location.IP2Location(ipv4database)
160+
rec = database.get_ascidr("8.8.8.8")
161+
assert rec == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.'
147162

148163
def testipv6countrycode():
149164
database = IP2Location.IP2Location(ipv6database)
@@ -158,4 +173,4 @@ def testipv6countryname():
158173
def testipv6unsupportedfield():
159174
database = IP2Location.IP2Location(ipv6database)
160175
rec = database.get_all("2001:4860:4860::8888")
161-
assert rec.city == None, "Test failed because city name not same."
176+
assert rec.city == 'This parameter is unavailable in selected .BIN data file. Please upgrade data file.', "Test failed because city name not same."

0 commit comments

Comments
 (0)