鏈接:http://blog.csdn.net/chuanchuan608/article/details/17915959
Keywords:
報錯:
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 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
socket.send(bytes[, flags]) 形參為字節類型
socket.recv(bufsize[, flags]) Receive datafrom the socket. The return value is abytes object representing the data received.
所以修正后代碼如下:
【woc看半天不懂代碼怎么寫】
【派森爸爸明明辣么可愛弱智!】
第二篇
http://blog.csdn.net/shanliangliuxing/article/details/7920400
第三篇
http://www.runoob.com/python3/python3-data-type.html
Python 中的變量不需要聲明。每個變量在使用前都必須賦值,變量賦值以后該變量才會被創建。
在 Python 中,變量就是變量,它沒有類型,我們所說的"類型"是變量所指的內存中對象的類型。
等號(=)用來給變量賦值。
等號(=)運算符左邊是一個變量名,等號(=)運算符右邊是存儲在變量中的值。
標準數據類型
Python3 中有六個標準的數據類型:
Number(數字)
String(字符串)
List(列表)
Tuple(元組)
Sets(集合)
Dictionary(字典
Number(數字)
Python3 支持 int、float、bool、complex(復數)。
在Python 3里,只有一種整數類型 int,表示為長整型,沒有 python2 中的 Long。
像大多數語言一樣,數值類型的賦值和計算都是很直觀的。
內置的 type() 函數可以用來查詢變量所指的對象類型。
此外還可以用 isinstance 來判斷
好惹,知道問題惹。
在python3.0以后的版本中,raw_input和input合體了,取消raw_input,并用input代替,所以現在的版本input接收的是字符串,你可以用:
x = int(input("x: "))
攤手。