• python3使用套接字遇到TypeError: 'str' does not support the buffer interface如何解决


    这是我查看的博客

    http://blog.csdn.net/chuanchuan608/article/details/17915959

    直接引用里面的关键语句:

    When you use client_socket.send(data),replace it by client_socket.send(data.encode()). When you get datausing data = client_socket.recv(512), replace it by data =client_socket.recv(512).decode()

    翻译过来,也就是说在你使用python3的时候,如果想要使用套接字组件send发送str类型的东西,记得先encode()一下。同理,当你想用recv接受str东西的时候,记得decode()一下。

    为什么会是这样呢?

    In python 3, bytes strings and unicodestrings are now two different types. Since sockets are not aware of string encodings, they are using raw bytes strings, that have a slightly differentinterface from unicode strings.

    So, now, whenever you have a unicode stringthat you need to use as a byte string, you need toencode() it. And whenyou have a byte string, you need to decode it to use it as a regular(python 2.x) string.

    Unicode strings are quotes enclosedstrings. Bytes strings are b"" enclosed strings

    原来在python3里面,bytes strings和unicode strings已经是不同的两个类型的(是不是说python2里面是同一类型?不清楚)。由于套接字并不关心字符串的编码方式,他们使用原字节字符串(raw bytes strings)进行传输,而这个类型又和unicode strings又有一点小小的不同。

    因此,现在任何时候你要是有一个unicode strings需要把它转化为byte string,你需要对他进行encode操作。当你有bytes string的时候,你需要decode操作。

    unicode strings =encode()=》 byte strings

    byte strings =decode()=》 unicode strings

  • 相关阅读:
    是河南大学的悲哀???
    装完manjaro先要卸载
    技术博客
    VIM从入门到中级教程
    HTTP中GET与POST的区别
    AngularJS 拦截器实现全局$http请求loading效果
    angular指令监听ng-repeat渲染完成后执行自定义事件方法
    icheck如何修改样式大小
    Sublime text3 代码格式化插件
    代理模式小试
  • 原文地址:https://www.cnblogs.com/Blaxon/p/4503518.html
Copyright © 2020-2023  润新知