mirror of
https://github.com/mjg59/python-broadlink.git
synced 2024-11-22 07:00:12 +01:00
Close sockets after using them (#350)
This commit is contained in:
parent
17968ef4d4
commit
3e8b008ef7
@ -138,6 +138,7 @@ def discover(timeout=None, local_ip_address=None, discover_ip_address='255.255.2
|
|||||||
name = responsepacket[0x40:].split(b'\x00')[0].decode('utf-8')
|
name = responsepacket[0x40:].split(b'\x00')[0].decode('utf-8')
|
||||||
cloud = bool(responsepacket[-1])
|
cloud = bool(responsepacket[-1])
|
||||||
device = gendevice(devtype, host, mac, name=name, cloud=cloud)
|
device = gendevice(devtype, host, mac, name=name, cloud=cloud)
|
||||||
|
cs.close()
|
||||||
return device
|
return device
|
||||||
|
|
||||||
while (time.time() - starttime) < timeout:
|
while (time.time() - starttime) < timeout:
|
||||||
@ -145,6 +146,7 @@ def discover(timeout=None, local_ip_address=None, discover_ip_address='255.255.2
|
|||||||
try:
|
try:
|
||||||
response = cs.recvfrom(1024)
|
response = cs.recvfrom(1024)
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
|
cs.close()
|
||||||
return devices
|
return devices
|
||||||
responsepacket = bytearray(response[0])
|
responsepacket = bytearray(response[0])
|
||||||
host = response[1]
|
host = response[1]
|
||||||
@ -154,6 +156,7 @@ def discover(timeout=None, local_ip_address=None, discover_ip_address='255.255.2
|
|||||||
cloud = bool(responsepacket[-1])
|
cloud = bool(responsepacket[-1])
|
||||||
device = gendevice(devtype, host, mac, name=name, cloud=cloud)
|
device = gendevice(devtype, host, mac, name=name, cloud=cloud)
|
||||||
devices.append(device)
|
devices.append(device)
|
||||||
|
cs.close()
|
||||||
return devices
|
return devices
|
||||||
|
|
||||||
|
|
||||||
@ -169,10 +172,6 @@ class device:
|
|||||||
self.iv = bytearray(
|
self.iv = bytearray(
|
||||||
[0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58])
|
[0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58])
|
||||||
self.id = bytearray([0, 0, 0, 0])
|
self.id = bytearray([0, 0, 0, 0])
|
||||||
self.cs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
self.cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
||||||
self.cs.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
||||||
self.cs.bind(('', 0))
|
|
||||||
self.type = "Unknown"
|
self.type = "Unknown"
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
|
|
||||||
@ -290,15 +289,20 @@ class device:
|
|||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
cs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
self.cs.sendto(packet, self.host)
|
cs.sendto(packet, self.host)
|
||||||
self.cs.settimeout(1)
|
cs.settimeout(1)
|
||||||
response = self.cs.recvfrom(2048)
|
response = cs.recvfrom(2048)
|
||||||
break
|
break
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
if (time.time() - start_time) > self.timeout:
|
if (time.time() - start_time) > self.timeout:
|
||||||
raise
|
raise
|
||||||
|
finally:
|
||||||
|
cs.close()
|
||||||
return bytearray(response[0])
|
return bytearray(response[0])
|
||||||
|
|
||||||
|
|
||||||
@ -1116,3 +1120,4 @@ def setup(ssid, password, security_mode):
|
|||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
sock.sendto(payload, ('255.255.255.255', 80))
|
sock.sendto(payload, ('255.255.255.255', 80))
|
||||||
|
sock.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user