1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-09-21 12:30:10 +02:00
python-broadlink/broadlink/helpers.py
2020-09-16 14:05:07 -07:00

21 lines
617 B
Python

"""Helper functions."""
import socket
from .exceptions import exception
def get_local_ip() -> str:
"""Try to determine the local IP address of the machine."""
# Useful for VPNs.
try:
local_ip_address = socket.gethostbyname(socket.gethostname())
if not local_ip_address.startswith('127.'):
return local_ip_address
except socket.gaierror:
raise exception(-4013) # DNS Error
# Connecting to UDP address does not send packets.
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(('8.8.8.8', 53))
return s.getsockname()[0]