Merge pull request #34 from eschava/master

allow to specify network interface for discover method
This commit is contained in:
Matthew Garrett 2016-12-25 11:07:54 +00:00 committed by GitHub
commit 877a7b7460

5
broadlink/__init__.py Executable file → Normal file
View file

@ -51,7 +51,8 @@ def gendevice(devtype, host, mac):
else: else:
return device(host=host, mac=mac) return device(host=host, mac=mac)
def discover(timeout=None): def discover(timeout=None, local_ip_address=None):
if local_ip_address is None:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 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 s.connect(('8.8.8.8', 53)) # connecting to a UDP address doesn't send packets
local_ip_address = s.getsockname()[0] local_ip_address = s.getsockname()[0]
@ -59,7 +60,7 @@ def discover(timeout=None):
cs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) cs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
cs.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) cs.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
cs.bind(('',0)) cs.bind((local_ip_address,0))
port = cs.getsockname()[1] port = cs.getsockname()[1]
starttime = time.time() starttime = time.time()