小编ken*_*596的帖子

TypeError:使用套接字时无法将“ bytes”对象转换为隐式str

因此,我正在尝试将代码从Python 2.7转换为Python 3,似乎有些变化。我正在尝试通过套接字接收二进制数据,但现在不起作用。这是我的代码。

编辑:我已经添加了我的发送代码。另外,我真的不喜欢它现在的工作方式,它过于复杂。如果可以的话,最好有一种更好的发送/接收数据的方法。

def recv(self):
    # Receive the length of the incoming message (unpack the binary data)
    dataLength = socket.ntohl(struct.unpack("I", self._recv(4))[0])

    # Receive the actual data
    return self._recv(dataLength)

def _recv(self, length):
    try:
        data = ''
        recvLen = 0
        while recvLen < length:
            newData = self.sock.recv(length-recvLen)

            if newData == '':
                self.isConnected = False
                raise exceptions.NetworkError(errors.CLOSE_CONNECTION, errno=errors.ERR_CLOSED_CONNECTION)

            data = data + newData # TypeError here
            recvLen += len(newData)

        return data
    except socket.error as se:
        raise exceptions.NetworkError(str(se))

def send(self, data):
    if type(data) …
Run Code Online (Sandbox Code Playgroud)

python sockets byte recv python-3.x

2
推荐指数
1
解决办法
6860
查看次数

标签 统计

byte ×1

python ×1

python-3.x ×1

recv ×1

sockets ×1