Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- Support [ipwho.is](https://ipwho.is/)
- Support [ipquery.io](http://api.ipquery.io/?format=json)
### Changed
## [0.4] - 2025-06-09
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Public IP and Location Info:

#### IPv4 API

ℹ️ `ipv4-api` valid choices: [`auto-safe`, `auto`, `ip-api.com`, `ipinfo.io`, `ip.sb`, `ident.me`, `tnedi.me`, `ipapi.co`, `ipleak.net`, `my-ip.io`, `ifconfig.co`, `reallyfreegeoip.org`, `freeipapi.com`, `myip.la`, `ipquery.io`]
ℹ️ `ipv4-api` valid choices: [`auto-safe`, `auto`, `ip-api.com`, `ipinfo.io`, `ip.sb`, `ident.me`, `tnedi.me`, `ipapi.co`, `ipleak.net`, `my-ip.io`, `ifconfig.co`, `reallyfreegeoip.org`, `freeipapi.com`, `myip.la`, `ipquery.io`, `ipwho.is`]

ℹ️ The default value: `auto-safe`

Expand Down
35 changes: 35 additions & 0 deletions ipspot/ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,36 @@ def _ipquery_io_ipv4(geo: bool=False, timeout: Union[float, Tuple[float, float]]
return {"status": False, "error": str(e)}


def _ipwho_is_ipv4(geo: bool=False, timeout: Union[float, Tuple[float, float]]=5
) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
"""
Get public IP and geolocation using ipwho.is.

:param geo: geolocation flag
:param timeout: timeout value for API
"""
try:
data = _get_json_ipv4_forced(url="https://ipwho.is", timeout=timeout)
result = {"status": True, "data": {"ip": data["ip"], "api": "ipwho.is"}}
if geo:
connection = data.get("connection", {})
timezone = data.get("timezone", {})
geo_data = {
"city": data.get("city"),
"region": data.get("region"),
"country": data.get("country"),
"country_code": data.get("country_code"),
"latitude": data.get("latitude"),
"longitude": data.get("longitude"),
"organization": connection.get("org"),
"timezone": timezone.get("id")
}
result["data"].update(geo_data)
return result
except Exception as e:
return {"status": False, "error": str(e)}


IPV4_API_MAP = {
IPv4API.IFCONFIG_CO: {
"thread_safe": False,
Expand Down Expand Up @@ -532,6 +562,11 @@ def _ipquery_io_ipv4(geo: bool=False, timeout: Union[float, Tuple[float, float]]
"geo": True,
"function": _ipquery_io_ipv4,
},
IPv4API.IPWHO_IS: {
"thread_safe": False,
"geo": True,
"function": _ipwho_is_ipv4,
},
}


Expand Down
1 change: 1 addition & 0 deletions ipspot/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class IPv4API(Enum):
MYIP_LA = "myip.la"
FREEIPAPI_COM = "freeipapi.com"
IPQUERY_IO = "ipquery.io"
IPWHO_IS = "ipwho.is"


PARAMETERS_NAME_MAP = {
Expand Down
20 changes: 20 additions & 0 deletions tests/test_ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ def test_public_ipv4_ipquery_io_net_error():
assert result["error"] == "No Internet"


def test_public_ipv4_ipwho_is_success():
result = get_public_ipv4(api=IPv4API.IPWHO_IS, geo=True)
assert result["status"]
assert is_ipv4(result["data"]["ip"])
assert set(result["data"].keys()) == DATA_ITEMS
assert result["data"]["api"] == "ipwho.is"


def test_public_ipv4_ipwho_is_timeout_error():
result = get_public_ipv4(api=IPv4API.IPWHO_IS, geo=True, timeout="5")
assert not result["status"]


def test_public_ipv4_ipwho_is_net_error():
with mock.patch.object(requests.Session, "get", side_effect=Exception("No Internet")):
result = get_public_ipv4(api=IPv4API.IPWHO_IS)
assert not result["status"]
assert result["error"] == "No Internet"


def test_public_ipv4_freeipapi_com_success():
result = get_public_ipv4(api=IPv4API.FREEIPAPI_COM, geo=True)
assert result["status"]
Expand Down