Skip to content

Commit b6e1a9d

Browse files
Supported Hosted Domain API.
1 parent a56060d commit b6e1a9d

File tree

12 files changed

+130
-9
lines changed

12 files changed

+130
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ dist/
88
htmlcov/
99
.tox/
1010
docs/_build/
11+
docs/build/
1112
*.egg-info

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 - 2024 IP2Location.io
3+
Copyright (c) 2023 - 2025 IP2Location.io
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
IP2Location.io Python SDK
22
========================
3+
![PyPI - Version](https://img.shields.io/pypi/v/ip2location-io)
4+
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ip2location-io)
5+
36
This Python module enables user to query for an enriched data set, such as country, region, city, latitude & longitude, ZIP code, time zone, ASN, ISP, domain, net speed, IDD code, area code, weather station data, MNC, MCC, mobile brand, elevation, usage type, address type, advertisement category, fraud score and proxy data with an IP address. It supports both IPv4 and IPv6 address lookup.
47

58
In addition, this module provides WHOIS lookup api that helps users to obtain domain information, WHOIS record, by using a domain name. The WHOIS API returns a comprehensive WHOIS data such as creation date, updated date, expiration date, domain age, the contact information of the registrant, mailing address, phone number, email address, nameservers the domain is using and much more.
69

10+
There is also a Hosted Domain API that allowing users to get the list of hosted domain names by IP address in real time. The REST API supports both IPv4 and IPv6 address lookup.
11+
712
This module requires API key to function. You may sign up for a free API key at https://www.ip2location.io/pricing.
813

914
Developer Documentation

docs/source/code.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,32 @@ Get domain extension from a URL or domain.
210210
:param string str: (Required) The URL or domain.
211211
:return: Returns the domain extension in text.
212212
:rtype: string
213+
```
214+
215+
## HostedDomain Class
216+
217+
```{py:class} HostedDomain(config)
218+
Initialize the HostedDomain class.
219+
220+
:param object config: (Required) The IP2Location.io Configuration object returned by Configuration class.
221+
```
222+
223+
```{py:function} lookup(ip_address, page)
224+
Retrieve a list of hosted domains for an IP address.
225+
226+
:param string ip_address: (Required) The IP address (IPv4 or IPv6).
227+
:param integer page: (Optional) Pagination result returns of the hosted domains. If unspecified, 1st page will be used.
228+
:::
229+
:return: Returns the domain list in JSON. Refer below table for the fields avaliable in the JSON
230+
:rtype: json
231+
232+
**RETURN FIELDS**
233+
| Parameter | Type | Description |
234+
|---|---|---|
235+
|ip|string|IP address.|
236+
|total_domains|integer|Total number of hosted domains found.|
237+
|page|integer|Current lookup page.|
238+
|per_page|integer|Number of domains displayed in the page.|
239+
|total_pages|integer|Total pages of the hosted domains.|
240+
|domains|array|Hosted domains of the lookup IP Address.|
213241
```

docs/source/conf.py

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

88
project = 'IP2Location.io Python SDK'
9-
copyright = '2024, IP2Location'
9+
copyright = '2025, IP2Location'
1010
author = 'IP2Location'
1111

1212
release = '1.0.0'
@@ -46,7 +46,7 @@
4646
# https://myst-parser.readthedocs.io/en/latest/configuration.html#setting-html-metadata
4747
myst_html_meta = {
4848
"description": "IP2Location.io Python library allows user to query for an enriched data set based on IP address and provides WHOIS lookup api that helps users to obtain domain information.",
49-
"keywords": "IP2Location, Geolocation, IP location, Python, WHOIS, domain",
49+
"keywords": "IP2Location, Geolocation, IP location, Python, WHOIS, domain, hosted domain",
5050
"google-site-verification": "DeW6mXDyMnMt4i61ZJBNuoADPimo5266DKob7Z7d6i4",
5151
}
5252

docs/source/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
IP2Location.io Python SDK
22
========================
3+
![PyPI - Version](https://img.shields.io/pypi/v/ip2location-io)
4+
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ip2location-io)
5+
36
This Python module enables user to query for an enriched data set, such as country, region, city, latitude & longitude, ZIP code, time zone, ASN, ISP, domain, net speed, IDD code, area code, weather station data, MNC, MCC, mobile brand, elevation, usage type, address type, advertisement category, fraud score and proxy data with an IP address. It supports both IPv4 and IPv6 address lookup.
47

58
In addition, this module provides WHOIS lookup api that helps users to obtain domain information, WHOIS record, by using a domain name. The WHOIS API returns a comprehensive WHOIS data such as creation date, updated date, expiration date, domain age, the contact information of the registrant, mailing address, phone number, email address, nameservers the domain is using and much more.
69

10+
There is also a Hosted Domain API that allowing users to get the list of hosted domain names by IP address in real time. The REST API supports both IPv4 and IPv6 address lookup.
11+
712
## Table of contents
813
```{eval-rst}
914
.. toctree::

docs/source/quickstart.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ This module requires API key to function. You may sign up for a free API key at
99
### PyPI Installation
1010

1111
Use the following command to install the module:
12+
1213
```Bash
1314
pip install ip2location-io
1415
```
1516

1617
### Arch Linux
1718

1819
For Arch Linux user, you can install the module using the following command:
20+
1921
```Bash
2022
git clone https://aur.archlinux.org/ip2location-io-python.git && cd ip2location-io-python
2123
export IP2LOCATION_API_KEY=YOUR_API_KEY
@@ -114,6 +116,22 @@ domainwhois = ip2locationio.DomainWHOIS(configuration)
114116
print(domainwhois.getdomainextension('example.com'))
115117
```
116118

119+
### Get Hosted Domain List
120+
121+
You can get the domains hosted within the IP using following codes:
122+
123+
```python
124+
import ip2locationio
125+
126+
# Configures IP2Location.io API key
127+
configuration = ip2locationio.Configuration('YOUR_API_KEY')
128+
hosteddomain = ip2locationio.HostedDomain(configuration)
129+
130+
# Get the list of the hosted domain on the IP.
131+
result = hosteddomain.lookup('8.8.8.8')
132+
print(result)
133+
```
134+
117135
## Testing
118136

119137
For testing, you can use `pytest` with the following steps:

ip2locationio/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from ip2locationio.configuration import Configuration
22
from ip2locationio.ipgeolocation import IPGeolocation
3-
from ip2locationio.domainwhois import DomainWHOIS
3+
from ip2locationio.domainwhois import DomainWHOIS
4+
from ip2locationio.hosteddomain import HostedDomain

ip2locationio/hosteddomain.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import socket
2+
from ip2locationio.common import urlencode, httprequest, is_valid_ip
3+
4+
# Windows version of Python does not provide it
5+
# for compatibility with older versions of Windows.
6+
if not hasattr(socket, 'inet_pton'):
7+
def inet_pton(t, addr):
8+
import ctypes
9+
a = ctypes.WinDLL('ws2_32.dll')
10+
in_addr_p = ctypes.create_string_buffer(b(addr))
11+
if t == socket.AF_INET:
12+
out_addr_p = ctypes.create_string_buffer(4)
13+
elif t == socket.AF_INET6:
14+
out_addr_p = ctypes.create_string_buffer(16)
15+
n = a.inet_pton(t, in_addr_p, out_addr_p)
16+
if n == 0:
17+
raise ValueError('Invalid address')
18+
return out_addr_p.raw
19+
socket.inet_pton = inet_pton
20+
21+
class HostedDomain:
22+
23+
def __init__(self,configuration):
24+
self.apikey = configuration.getapikey()
25+
self.moduleversion = configuration.getmoduleversion()
26+
27+
def lookup(self,ip,page=''):
28+
'''This function will lookup hosted domain information for an IP address.'''
29+
parameters = urlencode((("key", self.apikey), ("ip", ip), ("format", "json"), ("page", page), ("source", "sdk-python-iplio"), ("source_version", self.moduleversion)))
30+
response = httprequest("domains.ip2whois.com", "/domains?", parameters)
31+
if (response == None):
32+
# return False
33+
raise IP2LocationIOAPIError('Hosted Domain lookup error.')
34+
if ('error' in response):
35+
raise IP2LocationIOAPIError(response['error']['error_message'])
36+
return response
37+
38+
class IP2LocationIOAPIError(Exception):
39+
"""Raise for IP2Location API Error Message"""

ip2locationio/ipgeolocation.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def inet_pton(t, addr):
2121
class IPGeolocation:
2222

2323
def __init__(self,configuration):
24-
# print('apikey: ' + configuration.getapikey())
2524
self.apikey = configuration.getapikey()
2625
self.moduleversion = configuration.getmoduleversion()
2726

@@ -33,9 +32,7 @@ def lookup(self,ip,language=''):
3332
# return False
3433
raise IP2LocationIOAPIError('IPGeolocation lookup error.')
3534
if ('error' in response):
36-
# if (('response' in response) and (response['response'] != 'OK')):
3735
raise IP2LocationIOAPIError(response['error']['error_message'])
38-
# return(response['response'])
3936
return response
4037

4138
class IP2LocationIOAPIError(Exception):

0 commit comments

Comments
 (0)