Python_bug收集_TypeError:'str' does not support the buffer interface

参考链接:http://blog.csdn.net/chuanchuan608/article/details/17915959
感谢原作者
如有侵权,立删

源文件:
server.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket
from time import ctime

HOST=socket.gethostname()
PORT=12345
BUFSIZE=1024
ADDR=(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 socket
from time import ctime

HOST=socket.gethostname()
PORT=12345
BUFSIZE=1024
ADDR=(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 socket
from time import ctime

HOST=socket.gethostname()
PORT=12345
BUFSIZE=1024
ADDR=(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 socket
from time import ctime

HOST=socket.gethostname()
PORT=12345
BUFSIZE=1024
ADDR=(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模块 都是同样的解决办法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值