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

21 lines
617 B
Python
Raw Normal View History

2020-09-15 03:33:59 +02:00
"""Helper functions."""
import socket
2020-09-16 03:55:37 +02:00
from .exceptions import exception
2020-09-15 03:33:59 +02:00
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
2020-09-16 03:55:37 +02:00
except socket.gaierror:
raise exception(-4013) # DNS Error
2020-09-15 03:33:59 +02:00
# 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]