Files
HenryPump-Drivers/flow-monitor/utilities.py
2025-07-09 13:49:03 -05:00

26 lines
759 B
Python

"""Utility functions for the driver."""
import socket
try:
import urllib
import contextlib
except:
pass
def get_private_ip_address():
"""Find the private IP Address of the host device."""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(("8.8.8.8", 80))
public_ip = sock.getsockname()[0]
sock.close()
return public_ip
def get_public_ip_address():
"""Find the public IP Address of the host device."""
ip_address = "0.0.0.0"
try:
with contextlib.closing(urllib.urlopen("http://checkip.amazonaws.com")) as url:
ip_address = url.read()
except Exception as e:
print("could not resolve check IP: {}".format(e))
return ip_address
return ip_address[:-1]