Merges and coding style improvements
This commit is contained in:
parent
2cbe0cfe70
commit
58f0ab6f1d
20
tplink-smartplug.py
Normal file → Executable file
20
tplink-smartplug.py
Normal file → Executable file
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python2
|
||||||
#
|
#
|
||||||
# TP-Link Wi-Fi Smart Plug Protocol Client
|
# TP-Link Wi-Fi Smart Plug Protocol Client
|
||||||
# For use with TP-Link HS-100 or HS-110
|
# For use with TP-Link HS-100 or HS-110
|
||||||
@ -18,20 +18,20 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
#
|
#
|
||||||
#
|
|
||||||
import socket
|
import socket
|
||||||
import argparse
|
import argparse
|
||||||
from struct import pack
|
from struct import pack
|
||||||
|
|
||||||
version = 0.1
|
version = 0.2
|
||||||
|
|
||||||
# Check if IP is valid
|
# Check if hostname is valid
|
||||||
def validIP(ip):
|
def validHostname(hostname):
|
||||||
try:
|
try:
|
||||||
socket.inet_pton(socket.AF_INET, ip)
|
socket.gethostbyname(hostname)
|
||||||
except socket.error:
|
except socket.error:
|
||||||
parser.error("Invalid IP Address.")
|
parser.error("Invalid hostname.")
|
||||||
return ip
|
return hostname
|
||||||
|
|
||||||
# Predefined Smart Plug Commands
|
# Predefined Smart Plug Commands
|
||||||
# For a full list of commands, consult tplink_commands.txt
|
# For a full list of commands, consult tplink_commands.txt
|
||||||
@ -71,12 +71,13 @@ def decrypt(string):
|
|||||||
|
|
||||||
# Parse commandline arguments
|
# Parse commandline arguments
|
||||||
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
|
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
|
||||||
parser.add_argument("-t", "--target", metavar="<ip>", required=True, help="Target IP Address", type=validIP)
|
parser.add_argument("-t", "--target", metavar="<hostname>", required=True, help="Target hostname or IP address", type=validHostname)
|
||||||
group = parser.add_mutually_exclusive_group(required=True)
|
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("-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("-j", "--json", metavar="<JSON string>", help="Full JSON string of command to send")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
# Set target IP, port and command to send
|
# Set target IP, port and command to send
|
||||||
ip = args.target
|
ip = args.target
|
||||||
port = 9999
|
port = 9999
|
||||||
@ -99,3 +100,4 @@ try:
|
|||||||
print "Received: ", decrypt(data[4:])
|
print "Received: ", decrypt(data[4:])
|
||||||
except socket.error:
|
except socket.error:
|
||||||
quit("Cound not connect to host " + ip + ":" + str(port))
|
quit("Cound not connect to host " + ip + ":" + str(port))
|
||||||
|
|
||||||
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 35 KiB |
Loading…
Reference in New Issue
Block a user