Python_bug收集_TypeError:'str' does not support the buffer interface
来源:互联网 发布:部落冲突八本升级数据 编辑:程序博客网 时间:2024/05/04 10:14
参考链接:http://blog.csdn.net/chuanchuan608/article/details/17915959
感谢原作者
如有侵权,立删
源文件:
server.py
#!/usr/bin/python# -*- coding: UTF-8 -*-import socketfrom time import ctimeHOST=socket.gethostname()PORT=12345BUFSIZE=1024ADDR=(HOST,PORT)tcpSerSock=socket.socket()tcpSerSock.bind(ADDR)tcpSerSock.listen(5)while True: print ("waitting for connection...") tcpCliSock,addr=tcpSerSock.accept() print('...connected from:', addr) while True: data=tcpCliSock.recv(BUFSIZE) if not data: break else: print ('recece data:from hostName=%s hostIp&Port=%s ' % (HOST,addr),data) tcpCliSock.send(('[%s] %s' % (ctime(),data))) tcpCliSock.close()tcpSerSock.close()
client.py
#!/usr/bin/python# -*- coding: UTF-8 -*-import socketfrom time import ctimeHOST=socket.gethostname()PORT=12345BUFSIZE=1024ADDR=(HOST,PORT)tcpCliSock=socket.socket()tcpCliSock.connect(ADDR)while True: data = input('please input > ') if not data: break tcpCliSock.send("data") data = tcpCliSock.recv(BUFSIZE) if not data: break print(data)tcpCliSock.close()
报错:
TypeError:’str’ does not support the buffer interface
网络大神帮助
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
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()
同时我看了一下python帮助文档:
Codec.encode(input[, errors])
Encodes the object input and returns atuple (output object, length consumed). Encoding converts a string object to abytes object using a particular character set encoding
Codec.decode(input[, errors])
Decodes the object input and returns atuple (output object, length consumed). Decoding converts a bytes objectencoded using a particular character set encoding to a string object.
input must be a bytes object or one whichprovides the read-only character buffer interface – for example, buffer objectsand memory mapped files.
修改后文件:
修改内容
send()内容追加.encode()
recv()内容追加.decode()
server.py
#!/usr/bin/python# -*- coding: UTF-8 -*-import socketfrom time import ctimeHOST=socket.gethostname()PORT=12345BUFSIZE=1024ADDR=(HOST,PORT)tcpSerSock=socket.socket()tcpSerSock.bind(ADDR)tcpSerSock.listen(5)while True: print ("waitting for connection...") tcpCliSock,addr=tcpSerSock.accept() print('...connected from:', addr) while True: data=tcpCliSock.recv(BUFSIZE).decode() if not data: break else: print ('recece data:from hostName=%s hostIp&Port=%s ' % (HOST,addr),data) tcpCliSock.send(('[%s] %s' % (ctime(),data)).encode()) tcpCliSock.close()tcpSerSock.close()
client.py
#!/usr/bin/python# -*- coding: UTF-8 -*-import socketfrom time import ctimeHOST=socket.gethostname()PORT=12345BUFSIZE=1024ADDR=(HOST,PORT)tcpCliSock=socket.socket()tcpCliSock.connect(ADDR)while True: data = input('please input > ') if not data: break tcpCliSock.send(data.encode()) data = tcpCliSock.recv(BUFSIZE).decode() if not data: break print(data) tcpCliSock.close()
同时 UDP 和socketserver模块 都是同样的解决办法
- Python_bug收集_TypeError:'str' does not support the buffer interface
- ‘str' does not support the buffer interface
- TypeError:'str' does not support the buffer interface
- TypeError:’str’ does not support the buffer interface
- Python 3 TypeError: ‘str’ does not support the buffer interface
- TypeError: 'str' does not support the buffer interface
- python3 ‘str’ does not support the buffer interface解决方案
- python ‘str’ does not support the buffer interface解决办法
- python socket send 错误:TypeError: 'str' does not support the buffer interface
- Python+client_connection.sendall(http_resp)+TypeError: 'str' does not support the buffer interface
- Python List find方法报错 TypeError: 'str' does not support the buffer interface
- python3中split分割字符串出现错误 TypeError‘str’ does not support the buffer interface
- Python笔记002——错误TypeError: 'str' does not support the buffer interface
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Command设计模式之监听器的设计
- Android开发本地及网络Mp3音乐播放器(十四)网络音乐下载功能实现
- HDU 1157 Who's in the Middle【水题】
- 集中式源代码版本控制器--SVN
- slab----1
- Python_bug收集_TypeError:'str' does not support the buffer interface
- 【Codeforces Round】351A - Bear and Game(水)
- 【codeforces】-#351A--Bear and Game(第一次cf,错到哭,还是有点开心的,嘿嘿)
- python(2)readlines()函数的注意问题
- 最简单的win32的窗口程序
- 解决使用Volley框架出现的乱码问题
- 6. ZigZag Conversion
- Ubuntu 安装 MongoDB
- hd2616 Kill the monster