【发布时间】: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