简体   繁体   中英

How to convert Bytes to String and then convert the same string back to bytes

I have a software that got 2 sides: a client and a server. The client in this software can connect to the server with IP and Port and then the client sends commands to the server and the server responds.

What I'm trying to do is to make a proxy in the client and a proxy in the server side because i want all those messages between client and server go through some IM that i built. So i connected the client to local port that my IM listens to.. andi did the same thing in the server side.

So the first message that my IM gets from this software in the client side is

b"DRPC\x02\x00\x00\x009V\xfe'\x07\x84GO\x83d\xee\x11\x8f\xb0\x8a\xc8\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00D\x00E\x00S\x00K\x00T\x00O\x00P\x00-\x00H\x00M\x00V\x005\x00U\x00R\x00U\x00\\\x00C\x00C\x00E\x00\x00\x00\x10\xce\xff\x0b\xd4\x01\x00\x00\x00\x00\x00\x00\xfd\x7f\x00\x00\x10\xce\xff\x0b\xd4\x01\x00\x00\xfc\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd4\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00~\xd1\xe8\xb6\xd1]\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00`\xcd\xa1\x12\xfd\x7f\x00\x00\xe0F\xfd\x99\xf7\x7f\x00\x00"

Which i assume is a bytes. To send it i convert it(my IM send and recieves strings so i have to convert it) to string using str(somethin)

In the server side i get this string and then i have to send it to the server's software in bytes format. but when i send it i don't get any response due to error, I tried the following things:

1)send this string as is - the error i got that it expects a bytes and not a string.

2) Tried to delete the prefix b" and the suffix " and then convert that string to bytes using .

bytes(str,'utf-8)

but the output of this command was

b"DRPC\\x02\\x00\\x00\\x009V\\xfe'\\x07\\x84GO\\x83d\\xee\\x11\\x8f\\xb0\\x8a\\xc8\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x80\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00D\\x00E\\x00S\\x00K\\x00T\\x00O\\x00P\\x00-\\x00H\\x00M\\x00V\\x005\\x00U\\x00R\\x00U\\x00\\\\\\x00C\\x00C\\x00E\\x00\\x00\\x00\\xd0\\xcf\\xff\\x0b\\xd4\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\xfd\\x7f\\x00\\x00\\xd0\\xcf\\xff\\x0b\\xd4\\x01\\x00\\x004\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xd4\\x01\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00~\\xd1\\xe8\\xb6\\xd1]\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00`\\xcd\\xa1\\x12\\xfd\\x7f\\x00\\x00\\xe0F\\xfd\\x99\\xf7\\x7f\\x00\\x00"

which is not the same bytes that i got in the server side.

Any suggestions on how to send/recieve those messages without any error ?

By the way, I tried to connect to the software in the server side manually(by creating a socket locally and send him the first command) and i got response so i can assume that the problem is in converting the bytes to string and vice versa.

Try encoding and decoding the strings before sending and when receiving.

For example:

Sending

userMessage = "String"
    s.send(userMessage.encode('utf-8'))

userMessage is encoded into bytes then sent to the server.

Receiving

 data = conn.recv(bufferSize)
        if not data: break
        messageRecv = data.decode('utf-8')

Server receives data and then creates string messageRecv by decoding the data message. Both using utf-8.

If it is a larger item such as a list; you would need to use pickle.dumps / pickle.loads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM