【问题标题】:Python throw error byte like object required not listPython抛出错误字节,如需要的对象而不是列表
【发布时间】:2020-12-04 07:51:26
【问题描述】:

我正在尝试通过以下代码连接套接字

    try:
        # create an INET, STREAMing socket
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # now connect to the web server on port 80 - the normal http port
        self.client.connect((host, port))
        user = ['User.loginByToken', access_token]
        self.client.sendall(user)
        # self.client._locust_environment = self.environment
    except Exception as e:
        color_print("\n[!] Check Server Address or Port", color="red", underline=True)

它会引发错误memoryview: a bytes-like object is required, not 'list'。我能做些什么来解决它?

【问题讨论】:

  • 这不是您得到的例外,但无论如何您只能通过套接字发送和接收字节,因此您必须将数据编码为类似字节的对象。编码 str 很容易,例如User.loginByToken'.encode('utf-8') 会给你一个 bytes 对象。我不知道access_token 是什么,所以你必须弄清楚如何对其进行编码。最好的解决方案可能是使用更高级别的协议,但如果没有更多细节,我无法提出任何建议。

标签: python-3.x list sockets memoryview


【解决方案1】:

您需要将用户变量转换为字节才能通过套接字发送: 你可以试试这个:

import json

user = json.dumps({'User.loginByToken': access_token})
user = user.encode("utf-8")

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多