我试图执行TPLINK smartplug python脚本:https://github.com/softScheck/tplink-smartplug/blob/master/tplink-smartplug.py

主要问题与“ sock_tcp.send(encrypt(cmd)) ”有关 . 变量cmd是我在终端中作为参数传递的字符串(参见下面的示例) . 使用Python2执行此代码我从smartplug获得了成功的响应

bash:~/tplink-smartplug-master$ python tplink-smartplug.py -t 192.168.xx.xx -j '{"emeter":{"get_realtime":{}}}'

输出 - 打印

Sent:     {"emeter":{"get_realtime":{}}} 

('Received: ', '{"emeter":{"get_realtime":{"current":0.039509,"voltage":236.064036,"power":4.880813,"total":0.004000,"err_code":0}}}')

如果我用Python3执行代码,我得到错误“ TypeError: a bytes-like object is required, not 'str' ” . 这与使用字符串的unicode而Python2使用字节的Python3有关 .

但是,如果我更改代码以适应Python3的要求:“4773171”强制从Unicode转换为字节 . 执行代码时的响应是:

bash:~/tplink-smartplug-master$ python3 tplink-smartplug.py -t 192.168.xx.xx -j '{"emeter":{"get_realtime":{}}}'

输出 - 打印

Sent:     {"emeter":{"get_realtime":{}}} 

Received:

Any ideas what kind of conversation should I do to make it working in Python3 ?

encrypt()函数只添加一个额外的图表,您可以在上面的链接上查看它 .

感谢您的帮助! J.P