2019-11-18 17:39:57 +00:00
|
|
|
import logging
|
|
|
|
import socket
|
2019-11-25 06:52:36 +00:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
|
|
|
|
from ipaddress import ip_address
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
from os.path import join as join_path
|
2019-12-14 15:23:31 +00:00
|
|
|
from . import logger
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
# TODO: Should be removed as soon as migration
|
|
|
|
# mechanism is finalized inside ucloud
|
|
|
|
def get_ipv4_address():
|
|
|
|
# If host is connected to internet
|
|
|
|
# Return IPv4 address of machine
|
|
|
|
# Otherwise, return 127.0.0.1
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
|
|
|
try:
|
|
|
|
s.connect(("8.8.8.8", 80))
|
|
|
|
except socket.timeout:
|
|
|
|
address = "127.0.0.1"
|
|
|
|
except Exception as e:
|
2019-12-14 15:23:31 +00:00
|
|
|
logger.exception(e)
|
2019-11-18 17:39:57 +00:00
|
|
|
address = "127.0.0.1"
|
|
|
|
else:
|
|
|
|
address = s.getsockname()[0]
|
|
|
|
|
|
|
|
return address
|
2019-11-25 06:52:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_ipv6_address():
|
|
|
|
try:
|
|
|
|
r = requests.get("https://api6.ipify.org?format=json")
|
|
|
|
content = json.loads(r.content.decode("utf-8"))
|
|
|
|
ip = ip_address(content["ip"]).exploded
|
|
|
|
except Exception as e:
|
2019-12-14 15:23:31 +00:00
|
|
|
logger.exception(e)
|
2019-11-25 06:52:36 +00:00
|
|
|
else:
|
|
|
|
return ip
|