From 460054ced7f9b4c13002fb96b8ef349aaa8f5770 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 9 Jan 2024 13:51:04 -1000 Subject: [PATCH] Avoid recreating struct each request in legacy protocol (#628) --- kasa/protocol.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kasa/protocol.py b/kasa/protocol.py index 47d4a90b..e86c07aa 100755 --- a/kasa/protocol.py +++ b/kasa/protocol.py @@ -31,6 +31,7 @@ from .json import loads as json_loads _LOGGER = logging.getLogger(__name__) _NO_RETRY_ERRORS = {errno.EHOSTDOWN, errno.EHOSTUNREACH, errno.ECONNREFUSED} +_UNSIGNED_INT_NETWORK_ORDER = struct.Struct(">I") def md5(payload: bytes) -> bytes: @@ -206,7 +207,7 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol): await self.writer.drain() packed_block_size = await self.reader.readexactly(self.BLOCK_SIZE) - length = struct.unpack(">I", packed_block_size)[0] + length = _UNSIGNED_INT_NETWORK_ORDER.unpack(packed_block_size)[0] buffer = await self.reader.readexactly(length) response = TPLinkSmartHomeProtocol.decrypt(buffer) @@ -311,7 +312,7 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol): :return: ciphertext to be send over wire, in bytes """ plainbytes = request.encode() - return struct.pack(">I", len(plainbytes)) + bytes( + return _UNSIGNED_INT_NETWORK_ORDER.pack(len(plainbytes)) + bytes( TPLinkSmartHomeProtocol._xor_payload(plainbytes) )