2
コードをPython 2.7からPython 3に変換しようとしていますが、何かが変更されたようです。私はソケット上でバイナリデータを受け取ろうとしていますが、今は動作しません。ここに私のコードです。TypeError:ソケットで作業中に暗黙的に 'bytes'オブジェクトをstrに変換できません
EDIT:送信コードを追加しました。また、私は実際に今のところうまくいかない、それは複雑すぎます。できる場合は、より良いデータの送受信方法があることが良いでしょう。あなたはバイトのデータを必要とするならば、
data = b''
を使用しなくて保つ文字列
data = data + newData.decode('utf-8')
# or
data = data + newData.decode('ascii')
にデコードする必要がありますので
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) is not str:
raise TypeError()
dataLength = len(data)
# Send the length of the message (int converted to network byte order and packed as binary data)
self._send(struct.pack("I", socket.htonl(dataLength)), 4)
# Send the actual data
self._send(data, dataLength)
def _send(self, data, length):
sentLen = 0
while sentLen < length:
try:
amountSent = self.sock.send(data[sentLen:])
except Exception:
self.isConnected = False
raise exceptions.NetworkError(errors.UNEXPECTED_CLOSE_CONNECTION)
if amountSent == 0:
self.isConnected = False
raise exceptions.NetworkError(errors.UNEXPECTED_CLOSE_CONNECTION)
sentLen += amountSent
常に置く** FULL **エラーメッセージ**。他にも有用な情報があります。どのラインが問題になるか。 – furas
http://python3porting.com/problems.html#bytes-strings-and-unicode – Amber
@furasを読んでみたいと思うかもしれません。質問にはコードを見れば必要なすべての情報が含まれています。エラーが発生します。エラーメッセージはタイトルにあります。あなたは他に何を探していますか? – ken596