python——TypeError: 'str' does not support the buffer interface
import socket
import sys port=51423
host="localhost" data=b"x"*10485760 #在字符串前加 b 是字符串变为bytes类。
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((host,port)) byteswritten=0
while byteswritten<len(data):
startpos = byteswritten
endpos = min(byteswritten+1024,len(data))
byteswritten+=sock.send(data[startpos:endpos])
sys.stdout.write("wrote %d bytes\r"% byteswritten)
sys.stdout.flush() sock.shutdown(1) print("All data sent.")
while True:
buf = sock.recv(1024).decode() #.decode()函数把bytes类型转化为str类型。
if not len(buf):
break
sys.stdout.write(buf)
问题解决:
In python 3, bytes strings and unicode strings are now two different types. Since sockets are not aware of string encodings, they are using raw bytes strings, that have a slightly different interface from unicode strings.
在python 3里,bytes类型和unicode字符串类型现在已经是两种不同的类型了。现在,socket的模块无法识别unicode str类型,它们现在使用bytes字节类。
So, now, whenever you have a unicode string that you need to use as a byte string, you need to encode() it.
And when you have a byte string, you need to decode it to use it as a regular(python 2.x) string.
所以,现在无论什么时候你有一个unicode str类但是你需要一个bytes类,你需要使用encode()函数转换它。
当你有一个bytes类型时,你需要用decode()函数转换它,作为一个普通unicode字符串。
Unicode strings are quotes enclosed strings. Bytes strings are b"" enclosed strings
unicode str类直接用引号“ *** ”包含就可以,bytes字符串则需要在引号“ ”前加b。b“ test string ”
When you use client_socket.send(data),replace it by client_socket.send(data.encode()).
当你使用socket.send(data)时,用socket.send(data.encode())代替。
When you get data using data = client_socket.recv(512),replace it by data =client_socket.recv(512).decode()
当你使用data=socket.recv(512)时,用socket.recv(512).decode()代替。
============================================知识扩展========================================
一、Python 3的bytes/str之别
原文:The bytes/str dichotomy in Python 3
了解了bytes/str之别,理解codecs模块就容易了。
Python
3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。
Python
3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别清晰。你不能拼接字符串和字节包,也无法在字节包里搜索字符串(反之亦然),也
不能将字符串传入参数为字节包的函数(反之亦然)。这是件好事。
不管怎样,字符串和字节包之间的界线是必然的,下面的图解非常重要,务请牢记于心:
字符串可以编码成字节包,而字节包可以解码成字符串。
>>> '€20'.encode('utf-8')
b'\xe2\x82\xac20'
>>> b'\xe2\x82\xac20'.decode('utf-8')
'€20'
这个问题要这么来看:字符串是文本的抽象表示。字符串由字符组成,字符则是与任何特定二进制表示无关的抽象实体。在操作字符串时,我们生活在幸福的 无知之中。我们可以对字符串进行分割和分片,可以拼接和搜索字符串。我们并不关心它们内部是怎么表示的,字符串里的每个字符要用几个字节保存。只有在将字 符串编码成字节包(例如,为了在信道上发送它们)或从字节包解码字符串(反向操作)时,我们才会开始关注这点。
传入encode和decode的参数是编码(或codec)。编码是一种用二进制数据表示抽象字符的方式。目前有很多种编码。上面给出的UTF-8是其中一种,下面是另一种:
>>> '€20'.encode('iso-8859-15')
b'\xa420'
>>> b'\xa420'.decode('iso-8859-15')
'€20'
编码是这个转换过程中至关重要的一部分。离了编码,bytes对象b'\xa420'只是一堆比特位而已。编码赋予其含义。采用不同的编码,这堆比特位的含义就会大不同:
>>> b'\xa420'.decode('windows-1255')
'₪20'
二、codecs 模块简介
codecs是encoders和decoders的缩写。
codecs模块为我们解决的字符编码的处理提供了lookup方法,它接受一个字符编码名称的参数,并返回指定字符编码对应的
codecs.CodecInfo 对象,该对象包含了
encoder、decoder、StreamReader和StreamWriter的函数对象和类对象的引用。为了简化对lookup方法的调用,
codecs还提供了getencoder(encoding)、getdecoder(encoding)、getreader(encoding)和
getwriter(encoding)方法;进一步,简化对特定字符编码的StreamReader、StreamWriter和
StreamReaderWriter的访问,codecs更直接地提供了open方法,通过encoding参数传递字符编码名称,即可获得对
encoder和decoder的双向服务。
这个模块的强大之处在于它提供了流的方式来处理字符串编码,当处理的数据很多时,这种方式很有用。
你可以使用IncrementalEncoder和IncrementalDecoder,但是强烈建议使用StreamReader和StreamWriter,因为使用它们会大大简化你的代码。
例如,有一个test.txt的文件,它的编码为gbk,现在我需要将它的编码转换为utf8,可以编写如下代码:
- #coding:utf8 2
- import codecs
- # 打开文件 如果此处用codecs.open()方法打开文件,就不用创建reader和writer
- fin = open('test.txt', 'r')
- fout = open('utf8.txt', 'w')
- # 获取 StreamReader
- reader = codecs.getreader('gbk')(fin)
- # 获取 StreamWriter
- writer = codecs.getwriter('utf8')(fout)
- din = reader.read(10)
- while din:
- writer.write(din)
- din = reader.read(10)
python——TypeError: 'str' does not support the buffer interface的更多相关文章
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
转自:http://blog.csdn.net/chuanchuan608/article/details/17915959 目前正在学习python,使用的工具为python3.2.3.发现3x版本 ...
- python编写telnet登陆出现TypeError:'str' does not support the buffer interface
python3支持byte类型,python2不支持.在python3中,telnet客户端向远程服务器发送的str要转化成byte,从服务器传过来的byte要转换成str,但是在python2不清楚 ...
- python3 TypeError: 'str' does not support the buffer interface in python
http://stackoverflow.com/questions/38714936/typeerror-str-does-not-support-the-buffer-interface-in-p ...
- python3使用套接字遇到TypeError: 'str' does not support the buffer interface如何解决
这是我查看的博客 http://blog.csdn.net/chuanchuan608/article/details/17915959 直接引用里面的关键语句: When you use clien ...
- python TypeError: 'str' object does not support item assignment”
想替换string里的空格,遍历替换提示如题错误,查询得知string类型不可更改 import string s = "2013/2/12" b = s.replace('/', ...
- Python的字符串修改报错:TypeError: 'str' object does not support item assignment
Python中想修改字符串的最后一个字符,使用name[-1] = 'e'来实现,运行后报错. 报错内容是:TypeError: 'str' object does not support item ...
- Python学习笔记1 -- TypeError: 'str' object is not callable
Traceback (most recent call last): File "myfirstpython.py", line 39, in <module> pri ...
- TypeError: 'str' object is not callable
Python报错TypeError: 'str' object is not callable
- Python之str()与repr()的区别
Python之str()与repr()的区别 str()一般是将数值转成字符串,主要面向用户. repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思.如list, ...
随机推荐
- HDU-3555
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- MySQL的数据类型和建库策略详解
无论是在小得可怜的免费数据库空间或是大型电子商务网站,合理的设计表结构.充分利用空间是十分必要的.这就要求我们对数据库系统的常用数据类型有充分的认识.下面我就将我的一点心得写出来跟大家分享. 一.数字 ...
- (2)Python 变量和运算符
一.python变量特点 python是弱类型语言,无需声明变量可以直接使用并且变量的数据类型可以动态改变 二.变量命名规则 1.不能使用python关键字 2.不能数字开头 3.不能包含空格 4.不 ...
- 洛谷 P3397 地毯 【二维差分标记】
题目背景 此题约为NOIP提高组Day2T1难度. 题目描述 在n*n的格子上有m个地毯. 给出这些地毯的信息,问每个点被多少个地毯覆盖. 输入输出格式 输入格式: 第一行,两个正整数n.m.意义如题 ...
- Codeforces 856B - Similar Words
856B - Similar Words 题意 如果一个字符串可以通过去掉首位字母得到另一个字符串,则称两个字符串相似. 给出一个字符串集合,求一个新的字符串集合,满足新集合里的字符串是原字符串集合中 ...
- NOIP2018提高组模拟题(五)
字符串(string) Description 小林与亮亮正在做一个游戏.小林随意地写出一个字符串,字符串只由大写 字母组成,然后指定一个非负整数 m,亮亮可以进行至多 m 次操作,每次操作 为交换相 ...
- dutacm.club 1094: 等差区间(RMQ区间最大、最小值,区间GCD)
1094: 等差区间 Time Limit:5000/3000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Others)Total ...
- rsync用于同步目录
rsync是unix/linux下同步文件的一个高效算法,它能同步更新两处计算机的文件与目录,并适当利用查找文件中的不同块以减少数据传输.rsync中一项与其他大部分类似程序或协定中所未见的重要特性是 ...
- Floyd-弗洛伊德算法
今天,研究一下谁都能看懂的弗洛伊德算法. 首先,弗洛伊德算法是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法. 这个算法需要一个用到一个二维数组啊a[][],而a[i][j]表示的就 ...
- [Codeforces 28D] Do not fear,DravDe is kind
Brief Intro: 对于四元组(v,c,l,r),求其子序列中v最大的和,并使其满足: 1.Ci+Li+Ri相同 2.L1=0,Rn=0 3.Li=Sigma(C1...Ci-1) Soluti ...