mirror of
https://github.com/mjg59/python-broadlink.git
synced 2024-11-11 02:10:12 +01:00
19 lines
543 B
Python
19 lines
543 B
Python
|
"""Helper functions."""
|
||
|
import socket
|
||
|
|
||
|
|
||
|
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 OSError:
|
||
|
pass
|
||
|
|
||
|
# 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]
|