From 22eac6aa45c32787fe5186ffc60d440878336be7 Mon Sep 17 00:00:00 2001 From: Victor Rachieru Date: Mon, 3 Jun 2019 21:49:01 +0300 Subject: [PATCH] Close socket properly --- tplink_smartplug/api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tplink_smartplug/api.py b/tplink_smartplug/api.py index e8b4b0b..bfedede 100644 --- a/tplink_smartplug/api.py +++ b/tplink_smartplug/api.py @@ -244,12 +244,14 @@ def command(self, cmd): if isinstance(cmd, dict): cmd = json.dumps(cmd) + sock = None try: sock = socket.create_connection((self.host, self.port), self.timeout) sock.send(self.encrypt(cmd)) data = sock.recv(4096) finally: - sock.close() + if sock: + sock.close() response = self.decrypt(data[4:]) response = json.loads(response)