Python - 将 sock.recv 转换为字符串 [英] Python - converting sock.recv to string

查看:61
本文介绍了Python - 将 sock.recv 转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Python 和网络.

I'm digging around with python and networking.

while True:
   data = sock.recv(10240)

这绝对是在听.不过好像需要转成文本字符串.

This is definitely listening. But it seems to need to be converted to a text string.

我见过一些人使用 struct.unpack(),但我不确定它究竟是如何工作的.有什么方法可以转换?

I've seen some people using struct.unpack(), but I'm not sure exactly how it works. What's the way to convert?

推荐答案

你从 recv 是一个 bytes 字符串:

从套接字接收数据.返回值是一个字节对象,表示接收到的数据.

Receive data from the socket. The return value is a bytes object representing the data received.

在 Python 3.x 中,转换 bytes 字符串转换为 Unicode 文本 str 字符串,你必须知道字符串是用什么字符集编码的,所以你可以调用 解码.例如,如果是 UTF-8:

In Python 3.x, to convert a bytes string into a Unicode text str string, you have to know what character set the string is encoded with, so you can call decode. For example, if it's UTF-8:

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

(在 Python 2.x 中,bytesstr 是一样的,所以你已经得到一个字符串.但是如果你想得到一个 Unicode 文本 unicode 字符串,它和 3.x 中的一样.)

(In Python 2.x, bytes is the same thing as str, so you've already got a string. But if you want to get a Unicode text unicode string, it's the same as in 3.x.)

人们经常使用 struct 的原因是数据不只是 8 位或 Unicode 文本,而是一些其他格式.例如,您可以将每条消息作为netstring"发送:一个长度(作为ASCII 数字)后跟一个 : 分隔符,然后是 length 个 UTF-8 字节,然后是一个 ,——例如 b"3:ABC,".(格式有多种变体,但这是 Bernstein 标准网络字符串.)

The reason people often use struct is that the data isn't just 8-bit or Unicode text, but some other format. For example, you might send each message as a "netstring": a length (as a string of ASCII digits) followed by a : separator, then length bytes of UTF-8, then a ,—such as b"3:Abc,". (There are variants on the format, but this is the Bernstein standard netstring.)

人们使用网络字符串或其他类似技术的原因是,当您使用 TCP 时,您需要某种方式来分隔消息.每个 recv 可以给你对方通过 send 传递的一半,或者它可以给你 3 个 send 和第四个的一部分.因此,您必须累积一个 recv 数据缓冲区,然后从中提取消息.并且您需要某种方式来判断一条消息何时结束以及下一条消息何时开始.如果您只是发送没有任何换行符的纯文本消息,则可以仅使用换行符作为分隔符.否则,您将不得不想出其他方法——可能是网络字符串,或者使用 \0 作为分隔符,或者使用换行符作为分隔符但转义数据中的实际换行符,或者使用一些自分隔的结构化格式,如 JSON.

The reason people use netstrings, or other similar techniques, is that you need some way to delimit messages when you're using TCP. Each recv could give you half of what the other side passed with send, or it could give your 3 sends and part of the 4th. So, you have to accumulate a buffer of recv data, and then pull the messages out of it. And you need some way to tell when one message ends and the next begins. If you're just sending plain text messages without any newlines, you can just use newlines as a delimiter. Otherwise, you'll have to come up with something else—maybe netstrings, or using \0 as a delimiter, or using newlines as a delimiter but escaping actual newlines within the data, or using some self-delimited structured format like JSON.

这篇关于Python - 将 sock.recv 转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆