Python - convert sock.recv to string

I delve into python and the net.

while True: data = sock.recv(10240) 

It is definitely listening. But it looks like it needs to be converted to a text string.

I saw some people use struct.unpack () , but I don’t know exactly how this works. What is the way to convert?

+7
source share
2 answers

What you will return from recv is the bytes line:

Get data from a socket. The return value is a byte object representing the received data.

In Python 3.x, to convert a bytes string to Unicode str text, you need to know which character sets the string encoding, so you can call decode . For example, if it is UTF-8:

 stringdata = data.decode('utf-8') 

(In Python 2.x, bytes is the same as str , so you already have a string. But if you want to get a text string in Unicode unicode format, it will be the same as in 3.y.)

The reason people often use struct is because the data is not only 8-bit or Unicode text, but also a different format. For example, you can send each message as " netstring ": length (as a string of ASCII digits), then : then length bytes UTF-8, then a, such as b"3:Abc," . (There are options in the format, but this is Bernstein's standard grid.)

The reason people use netstrings or other similar methods is because you need to somehow differentiate messages when you use TCP. Each recv can give you half what the other side sent with send , or it can give you 3 send and part four. So you have to accumulate the recv data buffer and then pull the messages out of it. And you need to somehow say when one message ends and the next begins. If you just send text messages without any newlines, you can simply use newline characters as a delimiter. Otherwise, you will have to come up with something else - maybe netstrings or use \0 as a delimiter or use newlines as a delimiter, but by avoiding the actual lines in the data or using some kind of structured format with self-separation, such as JSON.

+15
source

In Python 2.7.x and earlier, data already a string. In Python 3.x, data is an object of bytes. To convert bytes to strings, use the decode() method. decode() will require a codec argument, such as 'utf-8'.

+3
source

All Articles