日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中typeerror怎么解决_Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法...

發布時間:2025/3/12 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中typeerror怎么解决_Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目前正在學習python,使用的工具為python3.2.3。發現3x版本和2x版本有些差異,在套接字編程時,困擾了我很久,先將python核心編程書中的例子

代碼如下:

服務器端:

#Echo server program

from socket import *

from time importctime

HOST= '' #Symbolic name meaning all available interfaces

PORT = 50007 #Arbitrary non-privileged port

BUFSIZE = 1024ADDR=(HOST, PORT)

tcpSerSock=socket(AF_INET, SOCK_STREAM)

tcpSerSock.bind(ADDR)

tcpSerSock.listen(5)whileTrue:print('waiting for connection...')

tcpCliSock, addr=tcpSerSock.accept()print('...connected from:', addr)whileTrue:

data=tcpCliSock.recv(BUFSIZE)if notdata:breaktcpCliSock.send(('[%s] %s' %(ctime(), data)))

tcpCliSock.close()

tcpSerSock.close()

客戶端

#Echo client program

from socket import*HOST= '127.0.0.1'PORT= 50007 #The same port as used by the server

BUFSIZE = 1024ADDR=(HOST, PORT)

tcpCliSock=socket(AF_INET, SOCK_STREAM)

tcpCliSock.connect(ADDR)whileTrue:

data= input('>')if notdata:breaktcpCliSock.send(data)

data=tcpCliSock.recv(BUFSIZE)if notdata:break

print(data)

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 abytesobject representing the data received.

所以修正后代碼如下:

服務器端:

#Echo server program

from socket import *

from time importctime

HOST= '' #Symbolic name meaning all available interfaces

PORT = 50007 #Arbitrary non-privileged port

BUFSIZE = 1024ADDR=(HOST, PORT)

tcpSerSock=socket(AF_INET, SOCK_STREAM)

tcpSerSock.bind(ADDR)

tcpSerSock.listen(5)whileTrue:print('waiting for connection...')

tcpCliSock, addr=tcpSerSock.accept()print('...connected from:', addr)whileTrue:

data=tcpCliSock.recv(BUFSIZE).decode()if notdata:breaktcpCliSock.send(('[%s] %s' %(ctime(), data)).encode())

tcpCliSock.close()

tcpSerSock.close()

客服端:

#Echo client program

from socket import*HOST= '127.0.0.1'PORT= 50007 #The same port as used by the server

BUFSIZE = 1024ADDR=(HOST, PORT)

tcpCliSock=socket(AF_INET, SOCK_STREAM)

tcpCliSock.connect(ADDR)whileTrue:

data= input('>')if notdata:breaktcpCliSock.send(data.encode())

data=tcpCliSock.recv(BUFSIZE).decode()if notdata:break

print(data)

tcpCliSock.close()

運行結果: 達到預期

在使用這些函數時想當然去用,沒有去查找幫助文檔,沒有弄清楚傳參類型,可能是一個例題,沒有注意這些,但是得吸取教訓。

同樣的在udp的情況下:修正過的

服務器端:

from socket import *

from time importctime

HOST= '';

PORT= 21546BUFSIZE= 1024ADDR=(HOST, PORT)

udpSerSock=socket(AF_INET, SOCK_DGRAM)

udpSerSock.bind(ADDR)whileTrue:print('waiting for message...')

data, addr=udpSerSock.recvfrom(BUFSIZE)

udpSerSock.sendto(('[%s] %s' %(ctime(), data.decode())).encode(), addr)print('...received from and returned to:', addr)

udpSerSock.close()

客戶端:

from socket import *HOST= 'localhost'PORT= 21567BUFSIZE= 1024ADDR=(HOST, PORT)whileTrue:

tcpCliSock=socket(AF_INET, SOCK_STREAM)

tcpCliSock.connect(ADDR)

data= input('>')if notdata:breaktcpCliSock.send(('%s\r\n' %data).encode())

data=tcpCliSock.recv(BUFSIZE).decode()if notdata:break

print(data.strip())

tcpCliSock.close()

使用socketserver模塊:

服務器端:

#TsTservss.py

from socketserver importTCPServer as TCP, StreamRequestHandler as SRHfrom time importctime

HOST= ''PORT= 21567ADDR=(HOST, PORT)classMyRequestHandler(SRH):defhandle(self):print('...connected from:', self.client_address)

self.wfile.write(('[%s] %s' %(ctime(), self.rfile.readline().decode())).encode())

tcpServ=TCP(ADDR, MyRequestHandler)print('waiting for connection...')

tcpServ.serve_forever()

客戶端:

from socket import *HOST= 'localhost'PORT= 21567BUFSIZE= 1024ADDR=(HOST, PORT)whileTrue:

tcpCliSock=socket(AF_INET, SOCK_STREAM)

tcpCliSock.connect(ADDR)

data= input('>')if notdata:breaktcpCliSock.send(('%s\r\n' %data).encode())

data=tcpCliSock.recv(BUFSIZE).decode()if notdata:break

print(data.strip())

tcpCliSock.close()

總結

以上是生活随笔為你收集整理的python中typeerror怎么解决_Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。