mirror of
https://github.com/softScheck/tplink-smartplug
synced 2026-01-11 23:38:46 +01:00
Dropped Python2 support; improved code style
This commit is contained in:
@@ -19,12 +19,11 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import sys
|
||||
import socket
|
||||
import argparse
|
||||
import socket
|
||||
from struct import pack
|
||||
|
||||
version = 0.3
|
||||
version = 0.4
|
||||
|
||||
# Check if hostname is valid
|
||||
def validHostname(hostname):
|
||||
@@ -41,7 +40,7 @@ def validPort(port):
|
||||
except ValueError:
|
||||
parser.error("Invalid port number.")
|
||||
|
||||
if ((port <= 1024) or (port >65535)) :
|
||||
if ((port <= 1024) or (port > 65535)):
|
||||
parser.error("Invalid port number.")
|
||||
|
||||
return port
|
||||
@@ -49,7 +48,7 @@ def validPort(port):
|
||||
|
||||
# Predefined Smart Plug Commands
|
||||
# For a full list of commands, consult tplink_commands.txt
|
||||
commands = { 'info' : '{"system":{"get_sysinfo":{}}}',
|
||||
commands = {'info' : '{"system":{"get_sysinfo":{}}}',
|
||||
'on' : '{"system":{"set_relay_state":{"state":1}}}',
|
||||
'off' : '{"system":{"set_relay_state":{"state":0}}}',
|
||||
'ledoff' : '{"system":{"set_led_off":{"off":1}}}',
|
||||
@@ -67,18 +66,17 @@ commands = { 'info' : '{"system":{"get_sysinfo":{}}}',
|
||||
|
||||
# Encryption and Decryption of TP-Link Smart Home Protocol
|
||||
# XOR Autokey Cipher with starting key = 171
|
||||
# Python 3.x Version
|
||||
if sys.version_info[0] > 2:
|
||||
def encrypt(string):
|
||||
|
||||
def encrypt(string):
|
||||
key = 171
|
||||
result = pack('>I', len(string))
|
||||
result = pack(">I", len(string))
|
||||
for i in string:
|
||||
a = key ^ ord(i)
|
||||
key = a
|
||||
result += bytes([a])
|
||||
return result
|
||||
|
||||
def decrypt(string):
|
||||
def decrypt(string):
|
||||
key = 171
|
||||
result = ""
|
||||
for i in string:
|
||||
@@ -87,35 +85,22 @@ if sys.version_info[0] > 2:
|
||||
result += chr(a)
|
||||
return result
|
||||
|
||||
# Python 2.x Version
|
||||
else:
|
||||
def encrypt(string):
|
||||
key = 171
|
||||
result = pack('>I', len(string))
|
||||
for i in string:
|
||||
a = key ^ ord(i)
|
||||
key = a
|
||||
result += chr(a)
|
||||
return result
|
||||
|
||||
def decrypt(string):
|
||||
key = 171
|
||||
result = ""
|
||||
for i in string:
|
||||
a = key ^ ord(i)
|
||||
key = ord(i)
|
||||
result += chr(a)
|
||||
return result
|
||||
|
||||
# Parse commandline arguments
|
||||
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
|
||||
parser.add_argument("-t", "--target", metavar="<hostname>", required=True, help="Target hostname or IP address", type=validHostname)
|
||||
parser.add_argument("-p", "--port", metavar="<port>", default=9999, required=False, help="Target port", type=validPort)
|
||||
parser.add_argument("-q", "--quiet", dest='quiet', action='store_true', help="Only show result")
|
||||
parser.add_argument("--timeout", default=10, required=False, help="Timeout to establish connection")
|
||||
parser = argparse.ArgumentParser(description=f"TP-Link Wi-Fi Smart Plug Client v{version}")
|
||||
parser.add_argument("-t", "--target", metavar="<hostname>", required=True,
|
||||
help="Target hostname or IP address", type=validHostname)
|
||||
parser.add_argument("-p", "--port", metavar="<port>", default=9999,
|
||||
required=False, help="Target port", type=validPort)
|
||||
parser.add_argument("-q", "--quiet", dest="quiet", action="store_true",
|
||||
help="Only show result")
|
||||
parser.add_argument("--timeout", default=10, required=False,
|
||||
help="Timeout to establish connection")
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument("-c", "--command", metavar="<command>", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands)
|
||||
group.add_argument("-j", "--json", metavar="<JSON string>", help="Full JSON string of command to send")
|
||||
group.add_argument("-c", "--command", metavar="<command>",
|
||||
help="Preset command to send. Choices are: "+", ".join(commands), choices=commands)
|
||||
group.add_argument("-j", "--json", metavar="<JSON string>",
|
||||
help="Full JSON string of command to send")
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
@@ -128,7 +113,6 @@ else:
|
||||
cmd = commands[args.command]
|
||||
|
||||
|
||||
|
||||
# Send command and receive reply
|
||||
try:
|
||||
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
@@ -148,4 +132,4 @@ try:
|
||||
print("Received: ", decrypted)
|
||||
|
||||
except socket.error:
|
||||
quit("Could not connect to host " + ip + ":" + str(port))
|
||||
quit(f"Could not connect to host {ip}:{port}")
|
||||
|
||||
Reference in New Issue
Block a user