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 @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Support [wtfismyip.com](https://wtfismyip.com/json) IPv6 API
- Support [myip.wtf](https://myip.wtf/) IPv6 API
- Support [myip.wtf](https://myip.wtf/) IPv4 API
- Support [db-ip.com](https://api.db-ip.com/v2/free/self) IPv4 API
### Changed
- `README.md` updated
## [0.7] - 2025-12-09
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,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`, `ipwho.is`, `wtfismyip.com`, `myip.wtf`]
ℹ️ `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`, `wtfismyip.com`, `myip.wtf`, `db-ip.com`]

ℹ️ The default value: `auto-safe`

Expand Down
39 changes: 36 additions & 3 deletions ipspot/ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def _wtfismyip_com_ipv4(geo: bool, timeout: Union[float, Tuple[float, float]]


def _myip_wtf_ipv4(geo: bool, timeout: Union[float, Tuple[float, float]]
) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
"""
Get public IP and geolocation using myip.wtf.

Expand All @@ -494,6 +494,34 @@ def _myip_wtf_ipv4(geo: bool, timeout: Union[float, Tuple[float, float]]
return {"status": False, "error": str(e)}


def _db_ip_com_ipv4(geo: bool, timeout: Union[float, Tuple[float, float]]
) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
"""
Get public IP and geolocation using db-ip.com.

:param geo: geolocation flag
:param timeout: timeout value for API
"""
try:
data = _get_json_force_ip(url="https://api.db-ip.com/v2/free/self", timeout=timeout, version="ipv4")
result = {"status": True, "data": {"ip": data["ipAddress"], "api": "db-ip.com"}}
if geo:
geo_data = {
"city": data.get("city"),
"region": data.get("stateProv"),
"country": data.get("countryName"),
"country_code": data.get("countryCode"),
"latitude": None, # not provided by free API
"longitude": None, # not provided by free API
"organization": None, # not provided by free API
"timezone": None # not provided by free API
}
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 @@ -575,11 +603,16 @@ def _myip_wtf_ipv4(geo: bool, timeout: Union[float, Tuple[float, float]]
"geo": True,
"function": _myip_wtf_ipv4
},
IPv4API.DB_IP_COM: {
"thread_safe": False,
"geo": True,
"function": _db_ip_com_ipv4
},
}


def get_public_ipv4(api: IPv4API=IPv4API.AUTO_SAFE, geo: bool=False,
timeout: Union[float, Tuple[float, float]]=5,
def get_public_ipv4(api: IPv4API = IPv4API.AUTO_SAFE, geo: bool = False,
timeout: Union[float, Tuple[float, float]] = 5,
max_retries: int = 0,
retry_delay: float = 1.0,
backoff_factor: float = 1.0) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
Expand Down
6 changes: 3 additions & 3 deletions ipspot/ipv6.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def _wtfismyip_com_ipv6(geo: bool, timeout: Union[float, Tuple[float, float]]


def _myip_wtf_ipv6(geo: bool, timeout: Union[float, Tuple[float, float]]
) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
"""
Get public IP and geolocation using myip.wtf.

Expand Down Expand Up @@ -407,8 +407,8 @@ def _myip_wtf_ipv6(geo: bool, timeout: Union[float, Tuple[float, float]]
}


def get_public_ipv6(api: IPv6API=IPv6API.AUTO_SAFE, geo: bool=False,
timeout: Union[float, Tuple[float, float]]=5,
def get_public_ipv6(api: IPv6API = IPv6API.AUTO_SAFE, geo: bool = False,
timeout: Union[float, Tuple[float, float]] = 5,
max_retries: int = 0,
retry_delay: float = 1.0,
backoff_factor: float = 1.0) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
Expand Down
1 change: 1 addition & 0 deletions ipspot/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class IPv4API(Enum):
IPWHO_IS = "ipwho.is"
WTFISMYIP_COM = "wtfismyip.com"
MYIP_WTF = "myip.wtf"
DB_IP_COM = "db-ip.com"


class IPv6API(Enum):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_ipv4_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,11 @@ def test_public_ipv4_myip_wtf_success():
assert is_ipv4(result["data"]["ip"])
assert set(result["data"].keys()) == DATA_ITEMS
assert result["data"]["api"] == "myip.wtf"


def test_public_ipv4_db_ip_com_success():
result = get_public_ipv4(api=IPv4API.DB_IP_COM, geo=True, timeout=40, max_retries=4, retry_delay=90, backoff_factor=1.1)
assert result["status"]
assert is_ipv4(result["data"]["ip"])
assert set(result["data"].keys()) == DATA_ITEMS
assert result["data"]["api"] == "db-ip.com"
63 changes: 12 additions & 51 deletions tests/test_ipv4_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ def test_get_private_ipv4_exception():
assert result["error"] == "Test error"





def test_public_ipv4_auto_timeout_error():
result = get_public_ipv4(api=IPv4API.AUTO, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -90,9 +87,6 @@ def test_public_ipv4_auto_net_error():
assert result["error"] == "All attempts failed."





def test_public_ipv4_auto_safe_timeout_error():
result = get_public_ipv4(api=IPv4API.AUTO_SAFE, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -105,9 +99,6 @@ def test_public_ipv4_auto_safe_net_error():
assert result["error"] == "All attempts failed."





def test_public_ipv4_ipapi_co_timeout_error():
result = get_public_ipv4(api=IPv4API.IPAPI_CO, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -120,9 +111,6 @@ def test_public_ipv4_ipapi_co_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_ipleak_net_timeout_error():
result = get_public_ipv4(api=IPv4API.IPLEAK_NET, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -135,9 +123,6 @@ def test_public_ipv4_ipleak_net_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_my_ip_io_timeout_error():
result = get_public_ipv4(api=IPv4API.MY_IP_IO, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -150,9 +135,6 @@ def test_public_ipv4_my_ip_io_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_ifconfig_co_timeout_error():
result = get_public_ipv4(api=IPv4API.IFCONFIG_CO, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -165,9 +147,6 @@ def test_public_ipv4_ifconfig_co_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_myip_la_timeout_error():
result = get_public_ipv4(api=IPv4API.MYIP_LA, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -180,9 +159,6 @@ def test_public_ipv4_myip_la_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_ipquery_io_timeout_error():
result = get_public_ipv4(api=IPv4API.IPQUERY_IO, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -195,9 +171,6 @@ def test_public_ipv4_ipquery_io_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_ipwho_is_timeout_error():
result = get_public_ipv4(api=IPv4API.IPWHO_IS, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -210,9 +183,6 @@ def test_public_ipv4_ipwho_is_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_freeipapi_com_timeout_error():
result = get_public_ipv4(api=IPv4API.FREEIPAPI_COM, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -225,9 +195,6 @@ def test_public_ipv4_freeipapi_com_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_ip_api_com_timeout_error():
result = get_public_ipv4(api=IPv4API.IP_API_COM, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -240,9 +207,6 @@ def test_public_ipv4_ip_api_com_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_ipinfo_io_timeout_error():
result = get_public_ipv4(api=IPv4API.IPINFO_IO, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -255,25 +219,18 @@ def test_public_ipv4_ipinfo_io_net_error():
assert result["error"] == "No Internet"





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



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





def test_public_ipv4_ident_me_timeout_error():
result = get_public_ipv4(api=IPv4API.IDENT_ME, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -286,9 +243,6 @@ def test_public_ipv4_ident_me_net_error():
assert result["error"] == "No Internet"





def test_public_ipv4_tnedi_me_timeout_error():
result = get_public_ipv4(api=IPv4API.TNEDI_ME, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -307,9 +261,6 @@ def test_public_ipv4_api_error():
assert result["error"] == "Unsupported API: api1"





def test_public_ipv4_reallyfreegeoip_org_timeout_error():
result = get_public_ipv4(api=IPv4API.REALLYFREEGEOIP_ORG, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -322,8 +273,6 @@ def test_public_ipv4_reallyfreegeoip_org_net_error():
assert result["error"] == "No Internet"




def test_public_ipv4_wtfismyip_com_timeout_error():
result = get_public_ipv4(api=IPv4API.WTFISMYIP_COM, geo=True, timeout="5")
assert not result["status"]
Expand All @@ -334,3 +283,15 @@ def test_public_ipv4_wtfismyip_com_net_error():
result = get_public_ipv4(api=IPv4API.WTFISMYIP_COM)
assert not result["status"]
assert result["error"] == "No Internet"


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


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