首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用加密功能将Python 2转换为3

使用加密功能将Python 2转换为3
EN

Stack Overflow用户
提问于 2019-07-29 00:45:17
回答 1查看 79关注 0票数 0

脚本是为Python2编写的,但我需要将它转换为Python3。

结果:

代码语言:javascript
复制
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:不能将str连接到字节

代码语言:javascript
复制
# 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
代码语言:javascript
复制
# 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))
EN

回答 1

Stack Overflow用户

发布于 2020-01-04 08:16:51

是的,它已经修改为Python3 at GITHUB

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57246029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档