mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-01-06 21:07:08 +00:00
Makes the socket sending code compatible with both Python 2 and python 3
Adds a shutdown to the socket used to send commands
This commit is contained in:
parent
77d524ecf2
commit
093899c588
@ -8,6 +8,7 @@ import socket
|
|||||||
import codecs
|
import codecs
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
|
import sys
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -228,16 +229,24 @@ class SmartPlug(object):
|
|||||||
a = key ^ ord(i)
|
a = key ^ ord(i)
|
||||||
key = a
|
key = a
|
||||||
result += chr(a)
|
result += chr(a)
|
||||||
|
|
||||||
|
if sys.version_info.major > 2:
|
||||||
|
return result.encode('latin-1')
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _decrypt(self, string):
|
def _decrypt(self, string):
|
||||||
"""Decrypts a command."""
|
"""Decrypts a command."""
|
||||||
|
if sys.version_info.major > 2:
|
||||||
|
string = string.decode('latin-1')
|
||||||
|
|
||||||
key = 171
|
key = 171
|
||||||
result = ""
|
result = ""
|
||||||
for i in string:
|
for i in string:
|
||||||
a = key ^ ord(i)
|
a = key ^ ord(i)
|
||||||
key = ord(i)
|
key = ord(i)
|
||||||
result += chr(a)
|
result += chr(a)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _send_command(self, command):
|
def _send_command(self, command):
|
||||||
@ -252,6 +261,7 @@ class SmartPlug(object):
|
|||||||
s.connect((self.ip, self.port))
|
s.connect((self.ip, self.port))
|
||||||
s.send(self._encrypt(command))
|
s.send(self._encrypt(command))
|
||||||
response = self._decrypt(s.recv(4096)[4:])
|
response = self._decrypt(s.recv(4096)[4:])
|
||||||
|
s.shutdown(1)
|
||||||
s.close()
|
s.close()
|
||||||
|
|
||||||
return json.loads(response)
|
return json.loads(response)
|
||||||
|
Loading…
Reference in New Issue
Block a user