Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法

来源:互联网 发布:win7建立网络共享 编辑:程序博客网 时间:2024/05/18 15:52

原帖地址:http://blog.csdn.net/chuanchuan608/article/details/17915959

目前正在学习python,使用的工具为python3.2.3。发现3x版本和2x版本有些差异,在套接字编程时,困扰了我很久,先将python核心编程书中的例子

代码如下:

服务器端:

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. # Echo server program  
  2. from socket import *  
  3. from time import ctime  
  4.   
  5. HOST = ''                 # Symbolic name meaning all available interfaces  
  6. PORT = 50007              # Arbitrary non-privileged port  
  7. BUFSIZE = 1024  
  8. ADDR = (HOST, PORT)  
  9.   
  10. tcpSerSock = socket(AF_INET, SOCK_STREAM)  
  11. tcpSerSock.bind(ADDR)  
  12. tcpSerSock.listen(5)  
  13.   
  14. while True:  
  15.     print('waiting for connection...')  
  16.     tcpCliSock, addr = tcpSerSock.accept()  
  17.     print('...connected from:', addr)  
  18.   
  19.     while True:  
  20.         data = tcpCliSock.recv(BUFSIZE)  
  21.         if not data:  
  22.             break  
  23.         tcpCliSock.send(('[%s] %s' % (ctime(), data)))  
  24.   
  25.         tcpCliSock.close()  
  26. tcpSerSock.close()  


客户端


[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. # Echo client program  
  2. from socket import*  
  3.   
  4. HOST = '127.0.0.1'  
  5. PORT = 50007              # The same port as used by the server  
  6. BUFSIZE = 1024  
  7. ADDR = (HOST, PORT)  
  8.   
  9. tcpCliSock = socket(AF_INET, SOCK_STREAM)  
  10. tcpCliSock.connect(ADDR)  
  11. while True:  
  12.     data = input('> ')  
  13.     if not data:  
  14.         break  
  15.     tcpCliSock.send(data)  
  16.     data = tcpCliSock.recv(BUFSIZE)  
  17.     if not data:  
  18.         break  
  19.     print(data)  
  20.       
  21. tcpCliSock.close()  

报错:

   TypeError:'str' does not support the buffer interface

找问题找了好久,在StackOverflow上发现有人也出现同样的问题,并一个叫Scharron的人提出了解答:

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 objectto 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

socket.send(bytes[, flags]) 形参为字节类型

socket.recv(bufsize[, flags]) Receive datafrom the socket. The return value is abytes object representing the data received.

 所以修正后代码如下:

服务器端:

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. # Echo server program  
  2. from socket import *  
  3. from time import ctime  
  4.   
  5. HOST = ''                 # Symbolic name meaning all available interfaces  
  6. PORT = 50007              # Arbitrary non-privileged port  
  7. BUFSIZE = 1024  
  8. ADDR = (HOST, PORT)  
  9.   
  10. tcpSerSock = socket(AF_INET, SOCK_STREAM)  
  11. tcpSerSock.bind(ADDR)  
  12. tcpSerSock.listen(5)  
  13.   
  14. while True:  
  15.     print('waiting for connection...')  
  16.     tcpCliSock, addr = tcpSerSock.accept()  
  17.     print('...connected from:', addr)  
  18.   
  19.     while True:  
  20.         data = tcpCliSock.recv(BUFSIZE).decode()  
  21.         if not data:  
  22.             break  
  23.         tcpCliSock.send(('[%s] %s' % (ctime(), data)).encode())  
  24.   
  25.         tcpCliSock.close()  
  26. tcpSerSock.close()  

客服端

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. # Echo client program  
  2. from socket import*  
  3.   
  4. HOST = '127.0.0.1'  
  5. PORT = 50007              # The same port as used by the server  
  6. BUFSIZE = 1024  
  7. ADDR = (HOST, PORT)  
  8.   
  9. tcpCliSock = socket(AF_INET, SOCK_STREAM)  
  10. tcpCliSock.connect(ADDR)  
  11. while True:  
  12.     data = input('> ')  
  13.     if not data:  
  14.         break  
  15.     tcpCliSock.send(data.encode())  
  16.     data = tcpCliSock.recv(BUFSIZE).decode()  
  17.     if not data:  
  18.         break  
  19.     print(data)  
  20.       
  21. tcpCliSock.close()  

运行结果: 达到预期

在使用这些函数时想当然去用,没有去查找帮助文档,没有弄清楚传参类型,可能是一个例题,没有注意这些,但是得吸取教训。

同样的在udp的情况下:修正过的

服务器端:

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. from socket import *  
  2. from time import ctime  
  3.   
  4. HOST = '';  
  5. PORT = 21546  
  6. BUFSIZE = 1024  
  7. ADDR = (HOST, PORT)  
  8.   
  9. udpSerSock = socket(AF_INET, SOCK_DGRAM)  
  10. udpSerSock.bind(ADDR)  
  11.   
  12. while True:  
  13.     print('waiting for message...')  
  14.     data, addr = udpSerSock.recvfrom(BUFSIZE)  
  15.     udpSerSock.sendto(('[%s] %s' %(ctime(), data.decode())).encode(), addr)  
  16.     print('...received from and returned to:', addr)  
  17.   
  18. udpSerSock.close()  

客户端:

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. from socket import *  
  2.   
  3. HOST = 'localhost'  
  4. PORT = 21567  
  5. BUFSIZE = 1024  
  6. ADDR = (HOST, PORT)  
  7.   
  8. while True:  
  9.     tcpCliSock = socket(AF_INET, SOCK_STREAM)  
  10.     tcpCliSock.connect(ADDR)  
  11.     data = input('> ')  
  12.     if not data:  
  13.         break  
  14.     tcpCliSock.send(('%s\r\n' % data).encode())  
  15.     data = tcpCliSock.recv(BUFSIZE).decode()  
  16.     if not data:  
  17.         break  
  18.     print(data.strip())  
  19. tcpCliSock.close()  

使用socketserver模块:

服务器端:

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #TsTservss.py  
  2. from socketserver import TCPServer as TCP, StreamRequestHandler as SRH  
  3. from time import ctime  
  4.   
  5.   
  6. HOST = ''  
  7. PORT = 21567  
  8. ADDR = (HOST, PORT)  
  9.   
  10. class MyRequestHandler(SRH):  
  11.     def handle(self):  
  12.         print('...connected from:'self.client_address)  
  13.         self.wfile.write(('[%s] %s' %(ctime(), self.rfile.readline().decode())).encode())  
  14.   
  15. tcpServ = TCP(ADDR, MyRequestHandler)  
  16. print('waiting for connection...')  
  17. tcpServ.serve_forever()  

客户端:

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. from socket import *  
  2.   
  3. HOST = 'localhost'  
  4. PORT = 21567  
  5. BUFSIZE = 1024  
  6. ADDR = (HOST, PORT)  
  7.   
  8. while True:  
  9.     tcpCliSock = socket(AF_INET, SOCK_STREAM)  
  10.     tcpCliSock.connect(ADDR)  
  11.     data = input('> ')  
  12.     if not data:  
  13.         break  
  14.     tcpCliSock.send(('%s\r\n' % data).encode())  
  15.     data = tcpCliSock.recv(BUFSIZE).decode()  
  16.     if not data:  
  17.         break  
  18.     print(data.strip())  
  19. tcpCliSock.close()  
0 0
原创粉丝点击