Python Socket Send Buffer Vs. Str

感情迁移 提交于 2019-11-27 14:28:13

It seems you try to use Python 2.x examples in Python 3 and you hit one of the main differences between those Python version.

For Python < 3 'strings' are in fact binary strings and 'unicode objects' are the right text objects (as they can contain any Unicode characters).

In Python 3 unicode strings are the 'regular strings' (str) and byte strings are separate objects.

Low level I/O can be done only with data (byte strings), not text (sequence of characters). For Python 2.x str was also the 'binary data' type. In Python 3 it is not any more and one of the special 'data' objects should be used. Objects are pickled to such byte strings. If you want to enter them manually in code use the "b" prefix (b"XXX" instead of "XXX").

To add to Jacek Konieczny's answer: You can also use str.encode() to get bytes from a string. If you have the string in a variable instead of a literal, you can call encode and it will return an equivalent series of bytes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!