mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-01-25 14:17:04 +00:00
Add timeout to query (#19)
This commit is contained in:
parent
fd4e363f56
commit
1e01530447
@ -39,20 +39,23 @@ class TPLinkSmartHomeProtocol:
|
|||||||
request = json.dumps(request)
|
request = json.dumps(request)
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
sock.connect((host, port))
|
sock.settimeout(5.0)
|
||||||
|
try:
|
||||||
|
sock.connect((host, port))
|
||||||
|
|
||||||
_LOGGER.debug("> (%i) %s", len(request), request)
|
_LOGGER.debug("> (%i) %s", len(request), request)
|
||||||
sock.send(TPLinkSmartHomeProtocol.encrypt(request))
|
sock.send(TPLinkSmartHomeProtocol.encrypt(request))
|
||||||
|
|
||||||
buffer = bytes()
|
buffer = bytes()
|
||||||
while True:
|
while True:
|
||||||
chunk = sock.recv(4096)
|
chunk = sock.recv(4096)
|
||||||
buffer += chunk
|
buffer += chunk
|
||||||
if not chunk:
|
if not chunk:
|
||||||
break
|
break
|
||||||
|
|
||||||
sock.shutdown(socket.SHUT_RDWR)
|
finally:
|
||||||
sock.close()
|
sock.shutdown(socket.SHUT_RDWR)
|
||||||
|
sock.close()
|
||||||
|
|
||||||
response = TPLinkSmartHomeProtocol.decrypt(buffer[4:])
|
response = TPLinkSmartHomeProtocol.decrypt(buffer[4:])
|
||||||
_LOGGER.debug("< (%i) %s", len(response), response)
|
_LOGGER.debug("< (%i) %s", len(response), response)
|
||||||
|
@ -103,7 +103,7 @@ class SmartPlug:
|
|||||||
request={target: {cmd: arg}}
|
request={target: {cmd: arg}}
|
||||||
)
|
)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
raise SmartPlugException(ex)
|
raise SmartPlugException(ex) from ex
|
||||||
|
|
||||||
result = response[target]
|
result = response[target]
|
||||||
if "err_code" in result and result["err_code"] != 0:
|
if "err_code" in result and result["err_code"] != 0:
|
||||||
|
Loading…
Reference in New Issue
Block a user