【发布时间】:2019-07-29 00:45:17
【问题描述】:
该脚本是为 Python 2 编写的,但我需要将其转换为 Python 3。当我这样做时,它会抛出此错误“TypeError: can't concat str to bytes”
结果:
Traceback (most recent call last):
File "tplink_smartplug.py", line 105, in <module>
sock_tcp.send(encrypt(cmd))
File "tplink_smartplug.py", line 70, in encrypt
result += chr(a)
TypeError: can't concat str to bytes
# XOR Autokey Cipher with starting key = 171
def encrypt(string):
key = 171
result = pack('>I', len(string))
for i in string:
a = key ^ ord(i)
key = a
result += chr(a) #line70
return result
def decrypt(string):
key = 171
result = ""
for i in string:
a = key ^ ord(i)
key = ord(i)
result += chr(a)
return result
# Send command and receive reply
try:
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_tcp.connect((ip, port))
sock_tcp.send(encrypt(cmd)) #line105
data = sock_tcp.recv(2048)
sock_tcp.close()
print(("Sent: ", cmd ))
print(("Received: ", decrypt(data[4:]) ))
except socket.error:
quit("Cound not connect to host " + ip + ":" + str(port))
【问题讨论】:
-
试试
result += chr(a).encode()或result += bytes( [a] )
标签: python encryption cmd tcp decode