mirror of
https://github.com/clearml/clearml
synced 2025-05-10 07:30:41 +00:00
Add api.public_ip_ping
(default: 8.8.8.8) and api.public_ip_service_urls
(default: ["api.ipify.org";, "ident.me";]) configuration settings to detect public IP and network interface
This commit is contained in:
parent
5c379928c6
commit
54418060ae
@ -34,7 +34,13 @@ def get_public_ip():
|
|||||||
|
|
||||||
:return: A string representing the IP of this machine or `None` if getting the IP failed
|
:return: A string representing the IP of this machine or `None` if getting the IP failed
|
||||||
"""
|
"""
|
||||||
for external_service in ["https://api.ipify.org", "https://ident.me"]:
|
from ..config import config_obj
|
||||||
|
# todo: add documentation in api section in conf file
|
||||||
|
public_ip_service_urls = (
|
||||||
|
config_obj.get("api.public_ip_service_urls", None)
|
||||||
|
or ["https://api.ipify.org", "https://ident.me"]
|
||||||
|
)
|
||||||
|
for external_service in public_ip_service_urls:
|
||||||
ip = get_public_ip_from_external_service(external_service)
|
ip = get_public_ip_from_external_service(external_service)
|
||||||
if ip:
|
if ip:
|
||||||
return ip
|
return ip
|
||||||
@ -72,10 +78,14 @@ def get_public_ip_from_external_service(external_service, timeout=5):
|
|||||||
|
|
||||||
|
|
||||||
def _get_private_ip_from_socket():
|
def _get_private_ip_from_socket():
|
||||||
|
from ..config import config_obj
|
||||||
|
# todo: add documentation in api section in conf file
|
||||||
|
public_ip_ping = config_obj.get("api.public_ip_ping", None) or "8.8.8.8"
|
||||||
|
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
s.settimeout(0)
|
s.settimeout(0)
|
||||||
try:
|
try:
|
||||||
s.connect(("8.8.8.8", 1))
|
s.connect((public_ip_ping, 1))
|
||||||
ip = s.getsockname()[0]
|
ip = s.getsockname()[0]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise e
|
raise e
|
||||||
|
Loading…
Reference in New Issue
Block a user