about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeonardo Brondani Schenkel <leonardo@schenkel.net>2019-08-16T09·13+0200
committerDaniel Høyer Iversen <mail@dahoiv.net>2019-08-16T09·13+0300
commit11c5981793f4a9b23ebceaf0ac2efdd74187c192 (patch)
treea0fdceaedb469dcd1ffa4159957eed6a6478dc37
parent1cea255dce6b966c1c7745a7a3327569735ed18e (diff)
Use old IP address lookup logic as fallback (#275)
On some machines, resolving the local hostname results in a loopback IP
address (127.0.0.0/8). This breaks discovery. In these situations, fall
back to the old IP address lookup logic that was removed on commit 790edb9.
-rw-r--r--broadlink/__init__.py4
1 files changed, 4 insertions, 0 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py
index 1517868784f1..8b08ffc5fee2 100644
--- a/broadlink/__init__.py
+++ b/broadlink/__init__.py
@@ -64,6 +64,10 @@ def gendevice(devtype, host, mac):
 def discover(timeout=None, local_ip_address=None):
     if local_ip_address is None:
         local_ip_address = socket.gethostbyname(socket.gethostname())
+    if local_ip_address.startswith('127.'):
+         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+         s.connect(('8.8.8.8', 53))  # connecting to a UDP address doesn't send packets
+         local_ip_address = s.getsockname()[0]
     address = local_ip_address.split('.')
     cs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)